使用基于属性值的lodash过滤对象数组

时间:2022-09-25 08:02:49

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?

如何从myArr获取对象列表,其名称是john与lodash?

6 个解决方案

#1


8  

Lodash has a "map" function that works just like jQuerys:

Lodash有一个“map”功能,就像jQuerys一样:

var myArr =  [{ name: "john", age:23 },
              { name: "john", age:43 },
              { name: "jimi", age:10 },
              { name: "bobi", age:67 }];

var johns = _.map(myArr, function(o) {
    if (o.name == "john") return o;
});

// Remove undefines from the array
johns = _.without(johns, undefined)

#2


78  

Use lodash _.filter method:

使用lodash _.filter方法:

_.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).

迭代集合的元素,返回所有元素的数组谓词返回truthy。使用三个参数调用谓词:(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)

谓词作为过滤对象的一部分(_.matches iteratee简写)

_.filter(myArr, {name: 'john'});

with predicate as [key, value] array (the _.matchesProperty iteratee shorthand.)

谓词为[key,value]数组(_.matchesProperty iteratee简写。)

_.filter(myArr, ['name', 'John']);

#3


1  

let myArr = [
    {name: "john", age:23},
    {name: "john", age:43},
    {name: "jim", age:101},
    {name: "bob", age:67},
];

// this will return old object (myArr) with items named 'john'
let list = _.filter(myArr, item => item.name === 'jhon');

//  this will return new object referenc (new Object) with items named 'john' 
let list = _.map(myArr, item => item.name === 'jhon').filter(item => item.name);

#4


0  

With lodash:

const myArr = [ {name: "john", age:23},
                {name: "john", age:43},
                {name: "jim", age:101},
                {name: "bob", age:67} ];

const johnArr = _.filter(myArr, person => person.name === 'john');
console.log(johnArr)

使用基于属性值的lodash过滤对象数组

Vanilla JavaScript:

const myArr = [ {name: "john", age:23},
                {name: "john", age:43},
                {name: "jim", age:101},
                {name: "bob", age:67} ];

const johnArr = myArr.filter(person => person.name === 'john');
console.log(johnArr);

使用基于属性值的lodash过滤对象数组

#5


0  

lodash also has a remove method

lodash还有一个删除方法

 var myArr = [ {name: "john", age:23}
          {name: "john", age:43}
          {name: "jim", age:101}
          {name: "bob", age:67} ];

 var onlyJohn = myArr.remove( person => { return person.name == "john" })

#6


0  


  
  
  
let myArr = [
    {name: "john", age:23},
    {name: "john", age:43},
    {name: "jim", age:101},
    {name: "bob", age:67},
];

let list = _.filter(myArr, item => item.name === "john");

#1


8  

Lodash has a "map" function that works just like jQuerys:

Lodash有一个“map”功能,就像jQuerys一样:

var myArr =  [{ name: "john", age:23 },
              { name: "john", age:43 },
              { name: "jimi", age:10 },
              { name: "bobi", age:67 }];

var johns = _.map(myArr, function(o) {
    if (o.name == "john") return o;
});

// Remove undefines from the array
johns = _.without(johns, undefined)

#2


78  

Use lodash _.filter method:

使用lodash _.filter方法:

_.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).

迭代集合的元素,返回所有元素的数组谓词返回truthy。使用三个参数调用谓词:(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)

谓词作为过滤对象的一部分(_.matches iteratee简写)

_.filter(myArr, {name: 'john'});

with predicate as [key, value] array (the _.matchesProperty iteratee shorthand.)

谓词为[key,value]数组(_.matchesProperty iteratee简写。)

_.filter(myArr, ['name', 'John']);

#3


1  

let myArr = [
    {name: "john", age:23},
    {name: "john", age:43},
    {name: "jim", age:101},
    {name: "bob", age:67},
];

// this will return old object (myArr) with items named 'john'
let list = _.filter(myArr, item => item.name === 'jhon');

//  this will return new object referenc (new Object) with items named 'john' 
let list = _.map(myArr, item => item.name === 'jhon').filter(item => item.name);

#4


0  

With lodash:

const myArr = [ {name: "john", age:23},
                {name: "john", age:43},
                {name: "jim", age:101},
                {name: "bob", age:67} ];

const johnArr = _.filter(myArr, person => person.name === 'john');
console.log(johnArr)

使用基于属性值的lodash过滤对象数组

Vanilla JavaScript:

const myArr = [ {name: "john", age:23},
                {name: "john", age:43},
                {name: "jim", age:101},
                {name: "bob", age:67} ];

const johnArr = myArr.filter(person => person.name === 'john');
console.log(johnArr);

使用基于属性值的lodash过滤对象数组

#5


0  

lodash also has a remove method

lodash还有一个删除方法

 var myArr = [ {name: "john", age:23}
          {name: "john", age:43}
          {name: "jim", age:101}
          {name: "bob", age:67} ];

 var onlyJohn = myArr.remove( person => { return person.name == "john" })

#6


0  


  
  
  
let myArr = [
    {name: "john", age:23},
    {name: "john", age:43},
    {name: "jim", age:101},
    {name: "bob", age:67},
];

let list = _.filter(myArr, item => item.name === "john");