扫码一下
查看教程更方便
_.where(list, properties)
where() 方法遍历给定的元素列表,调用每个元素的predicate。 它返回属性中所有匹配的键值对。
查看列表中的每个值,返回与属性中列出的键值对匹配的所有值的数组。
var _ = require('underscore');
var list = [{"title": "learn java", "author": "sam", "cost": 100},
{"title": "learn scala", "author": "joe", "cost": 200},
{"title": "learn c", "author": "sam", "cost": 200} ]
//示例 1. 查找作者为 sam 的对象
var result = _.where(list, { "author": "sam" });
console.log(result);
//示例 2. 查找花费200的对象
var result = _.where(list, { "cost": 200 });
上面示例运行结果如下