根据属性值过滤lodash对象的数组 [英] Filtering array of objects with lodash based on property value

查看:2478
本文介绍了根据属性值过滤lodash对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var myArr = [{name:john,age:23} 
{name:john,age:43}
{name:jim,年龄:101}
{姓名:bob,年龄:67}];

如何从myArr获取对象列表,其中名称是jood with lodash?

解决方案

使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆