Javascript 检查对象数组中是否有具有特定值的键的对象

Javascript 检查对象数组中是否有具有特定值的键的对象,javascript,jquery,underscore.js,Javascript,Jquery,Underscore.js,通过使用下划线或jQuery是否有更好的方法来检查对象数组[{id:1,name:'some name'},{id:2,name:'other name'}]是否包含特定的属性值 这是我的方式: _.filter([{id: 1}, {id: 2}], function(obj){ return obj.id === 3 }).length === 1; // false _.filter([{id: 1}, {id: 2}], function(obj){ ret

通过使用
下划线
jQuery
是否有更好的方法来检查对象数组
[{id:1,name:'some name'},{id:2,name:'other name'}]
是否包含特定的属性值

这是我的方式:

_.filter([{id: 1}, {id: 2}], 
      function(obj){ return obj.id === 3 }).length === 1; // false

_.filter([{id: 1}, {id: 2}], 
      function(obj){ return obj.id === 2 }).length === 1; // true

如果您不需要对象本身,只需要检查它是否在列表中:

_.contains(_.pluck([{id: 1}, {id: 2}], 'id'), 1) // => true
文件:,

我也是这样做的。找不到更好的方法。