如果我有以下对象数组:

[ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]

是否有一种方法通过数组循环检查特定的用户名值是否已经存在,如果它不做任何事情,但如果它没有添加一个新对象到数组的用户名(和新ID)?

谢谢!


当前回答

请看下面的例子

$(document).ready(function(){ const arr = document.querySelector(".list"); var abcde = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' }]; $("#btnCheckUser").click(function() { var tbUsername = $("#tbUsername").val(); if (abcde.some(obj => obj.username === tbUsername)) { alert('existing user ' + tbUsername); return; } else { abcde.push({ id: abcde.length + 1, username: tbUsername }); alert('added new user ' + tbUsername); arr.appendChild(createArray(tbUsername)); return; } }); function createArray(name) { let li = document.createElement("li"); li.textContent = name; return li; } abcde.forEach((x) => arr.appendChild(createArray(x.username))); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <p>Add text and click on Check.</p> <input type="text" id="tbUsername" /> <button type="button" id="btnCheckUser">Check</button> <div class="list"> <ul></ul> </div>

其他回答

检查现有的用户名相当简单:

var arr = [{ id: 1, username: 'fred' }, 
  { id: 2, username: 'bill'}, 
  { id: 3, username: 'ted' }];

function userExists(username) {
  return arr.some(function(el) {
    return el.username === username;
  }); 
}

console.log(userExists('fred')); // true
console.log(userExists('bred')); // false

但是当你必须向这个数组中添加一个新用户时,要做什么就不那么明显了。最简单的方法-只是推入一个id等于array的新元素。长度+ 1:

function addUser(username) {
  if (userExists(username)) {
    return false; 
  }
  arr.push({ id: arr.length + 1, username: username });
  return true;
}

addUser('fred'); // false
addUser('bred'); // true, user `bred` added

它将保证id的唯一性,但如果将一些元素从数组末尾删除,则会使该数组看起来有点奇怪。

我假设这里的id是唯一的。Find是一个很棒的数组方法,用于检查数组中是否存在东西:

Const arr = [{id: 1,用户名:'fred'}, {id: 2,用户名:'bill'}, {id: 3,用户名:'ted'}]; 函数add(arr, name) { Const {length} = arr; Const id =长度+ 1; Const found = arr。求(el => el。用户名=== name); 如果(!发现)arr。推送({id,用户名:name}); 返回arr; } console.log(添加(arr“ted”)); console.log(添加(加勒比海盗,“黛西”));

试试这个

第一种方法使用一些

  let arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
    let found = arr.some(ele => ele.username === 'bill');
    console.log(found)

第二种方法使用包括、映射

   let arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];
    let mapped = arr.map(ele => ele.username);
    let found = mapped.includes('bill');
    console.log(found)

这是我在@sagar-gavhane的回答之外所做的

const newUser = {_id: 4, name: 'Adam'}
const users = [{_id: 1, name: 'Fred'}, {_id: 2, name: 'Ted'}, {_id: 3, name:'Bill'}]

const userExists = users.some(user => user.name === newUser.name);
if(userExists) {
    return new Error({error:'User exists'})
}
users.push(newUser)

假设我们有一个对象数组,你想检查value of name是否像这样定义,

let persons = [ {"name" : "test1"},{"name": "test2"}];

if(persons.some(person => person.name == 'test1')) {
    ... here your code in case person.name is defined and available
}