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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35182904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:21:51  来源:igfitidea点击:

Filtering array of objects with lodash based on property value

javascriptfilterfilteringlodash

提问by sarsnake

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 的对象列表?

采纳答案by Dustin Poissant

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

Lodash 有一个类似于 jQuery 的“map”函数:

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)

回答by Enver Dzhaparoff

Use lodash _.filtermethod:

使用 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).

迭代集合的元素,返回所有元素的数组,谓词返回真值。谓词使用三个参数调用:(值、索引|键、集合)。

with predicate as custom function

将谓词作为自定义函数

 _.filter(myArr, function(o) { 
    return o.name == 'john'; 
 });

with predicate as part of filtered object(the _.matchesiteratee shorthand)

将谓词作为过滤对象的一部分_.matchesiteratee 简写)

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

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

with predicate as [key, value] 数组_.matchesPropertyiteratee 简写。)

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

回答by Yuci

With lodash:

使用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)

enter image description here

在此处输入图片说明

Vanilla JavaScript:

原生 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);

enter image description here

在此处输入图片说明

回答by Nver Abgaryan

**Filter by name, age ** also, you can use the map function

**按姓名、年龄过滤**也可以使用地图功能

difference between map and filter

地图和过滤器的区别

1. map- The map() method creates a new array with the results of calling a function for every array element. The map method allows items in an array to be manipulated to the user's preference, returning the conclusion of the chosen manipulation in an entirely new array. For example, consider the following array:

1. map- map() 方法使用为每个数组元素调用函数的结果创建一个新数组。map 方法允许根据用户的偏好操作数组中的项目,在一个全新的数组中返回所选操作的结论。例如,考虑以下数组:

2. filter- The filter() method creates an array filled with all array elements that pass a test implemented by the provided function. The filter method is well suited for particular instances where the user must identify certain items in an array that share a common characteristic. For example, consider the following array:

2. filter- filter() 方法创建一个数组,其中填充了所有通过所提供函数实现的测试的数组元素。filter 方法非常适用于用户必须识别数组中具有共同特征的某些项目的特定情况。例如,考虑以下数组:

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

const user = _.filter(users, {name: 'jim', age: 101});
console.log(user);

回答by Nver Abgaryan

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");

回答by Nver Abgaryan

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);

回答by Nicole Wheeler

lodash also has a remove method

lodash 还有一个 remove 方法

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" })