我正在尝试检查数组中是否已经存在具有特定 id 属性的对象:
var items =[{id:1,name:'ted'},{id:2,name:'john'}]
我怎样才能以紧凑的方式使用 jquery 或 vanilla js 来做到这一点?我知道我可以创建一个 for 循环,但您还有什么可以使用的吗?
最佳答案
使用array.prototype.some:
var items =[{id:1,name:'ted'},{id:2,name:'john'},{id:3, name:'tod'}];
var id = 2;
var exists = items.some(item => item.id === id)
console.log(exists);
关于javascript - 如何检查 id 值是否已经存在于对象数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47266108/