根据属性值过滤lodash对象的数组 [英] Filtering array of objects with lodash based on property value
问题描述
var myArr = [{name:john,age:23}
{name:john,age:43}
{name:jim,年龄:101}
{姓名:bob,年龄:67}];
如何从myArr获取对象列表,其中名称是jood with lodash?
_。filter
方法: _。filter(collection,[predicate = _。identity])
迭代集合的元素,返回谓词返回truthy的所有元素的数组。谓词被调用三个参数:
(value,index | key,collection)。
谓词作为自定义函数
_.filter(myArr,function(o){
return o.name =='john';
});
谓词作为过滤对象的一部分 > _。匹配 iteratee简写)
_。filter(myArr,{name:'john' });
谓词为[key,value]数组
$ b $ $ p $ _。filter(myArr,['name; $ c> _。matchesProperty
iteratee shorthand。)
We have an array of objects as such
var myArr = [ {name: "john", age:23}
{name: "john", age:43}
{name: "jim", age:101}
{name: "bob", age:67} ];
how do I get the list of objects from myArr where name is john with lodash?
Use lodash _.filter
method:
_.filter(collection, [predicate=_.identity])
Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).
with predicate as custom function
_.filter(myArr, function(o) {
return o.name == 'john';
});
with predicate as part of filtered object (the _.matches
iteratee shorthand)
_.filter(myArr, {name: 'john'});
with predicate as [key, value] array (the _.matchesProperty
iteratee shorthand.)
_.filter(myArr, ['name', 'John']);
这篇关于根据属性值过滤lodash对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!