简体   繁体   English

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

[英]Filtering array of objects with lodash based on property value

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

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).谓词使用三个参数调用:(值、索引|键、集合)。

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.) with predicate as [key, value] 数组_.matchesProperty iteratee 简写。)

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

Docs reference: https://lodash.com/docs/4.17.4#filter文档参考: https : //lodash.com/docs/4.17.4#filter

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)

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)

在此处输入图片说明

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

在此处输入图片说明

**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. 1. map - map() 方法使用为每个数组元素调用函数的结果创建一个新数组。 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. map 方法允许根据用户的偏好操作数组中的项目,在一个全新的数组中返回所选操作的结论。 For example, consider the following array:例如,考虑以下数组:

2. filter - The filter() method creates an array filled with all array elements that pass a test implemented by the provided function. 2. filter - filter() 方法创建一个数组,其中填充了所有通过所提供函数实现的测试的数组元素。 The filter method is well suited for particular instances where the user must identify certain items in an array that share a common characteristic. filter 方法非常适用于用户必须识别数组中具有共同特征的某些项目的特定情况。 For example, consider the following array:例如,考虑以下数组:

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

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


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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM