如果我有以下对象数组:
[ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]
是否有一种方法通过数组循环检查特定的用户名值是否已经存在,如果它不做任何事情,但如果它没有添加一个新对象到数组的用户名(和新ID)?
谢谢!
如果我有以下对象数组:
[ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]
是否有一种方法通过数组循环检查特定的用户名值是否已经存在,如果它不做任何事情,但如果它没有添加一个新对象到数组的用户名(和新ID)?
谢谢!
当前回答
接受的答案也可以这样写,在。some上使用箭头函数
function checkAndAdd(name) {
var id = arr.length + 1;
var found = arr.some((el) => {
return el.username === name;
});
if (!found) { arr.push({ id: id, username: name }); }
}
其他回答
接受的答案也可以这样写,在。some上使用箭头函数
function checkAndAdd(name) {
var id = arr.length + 1;
var found = arr.some((el) => {
return el.username === name;
});
if (!found) { arr.push({ id: id, username: name }); }
}
我假设这里的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(添加(加勒比海盗,“黛西”));
这是我在@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)
可以有多种可能的方法来检查一个元素是否在 你的案例它的对象)是否存在于数组中。
const arr = [
{ id: 1, username: 'fred' },
{ id: 2, username: 'bill' },
{ id: 3, username: 'ted' },
];
假设你想找一个id = 3的对象。
1. 发现: 它在数组中搜索一个元素,如果找到了,就返回该元素,否则返回undefined。它返回所提供数组中满足所提供测试函数的第一个元素的值。参考
const ObjIdToFind = 5;
const isObjectPresent = arr.find((o) => o.id === ObjIdToFind);
if (!isObjectPresent) { // As find return object else undefined
arr.push({ id: arr.length + 1, username: 'Lorem ipsum' });
}
2. 过滤器: 它搜索数组中的元素,并过滤掉所有符合条件的元素。它返回一个包含所有元素的新数组,如果没有符合条件,则返回空数组。参考
const ObjIdToFind = 5;
const arrayWithFilterObjects= arr.filter((o) => o.id === ObjIdToFind);
if (!arrayWithFilterObjects.length) { // As filter return new array
arr.push({ id: arr.length + 1, username: 'Lorem ipsum' });
}
3.一些: some()方法测试数组中是否至少有一个元素通过了所提供函数实现的测试。它返回一个布尔值。参考
const ObjIdToFind = 5;
const isElementPresent = arr.some((o) => o.id === ObjIdToFind);
if (!isElementPresent) { // As some return Boolean value
arr.push({ id: arr.length + 1, username: 'Lorem ipsum' });
}
我喜欢Andy的回答,但是id不一定是唯一的,所以这里是我想出的创建唯一id的方法。也可以在jsfiddle上查看。请注意arr。如果之前已经删除了任何内容,length + 1可能无法保证唯一的ID。
var array = [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' } ];
var usedname = 'bill';
var newname = 'sam';
// don't add used name
console.log('before usedname: ' + JSON.stringify(array));
tryAdd(usedname, array);
console.log('before newname: ' + JSON.stringify(array));
tryAdd(newname, array);
console.log('after newname: ' + JSON.stringify(array));
function tryAdd(name, array) {
var found = false;
var i = 0;
var maxId = 1;
for (i in array) {
// Check max id
if (maxId <= array[i].id)
maxId = array[i].id + 1;
// Don't need to add if we find it
if (array[i].username === name)
found = true;
}
if (!found)
array[++i] = { id: maxId, username: name };
}