简体   繁体   English

Javascript按对象的属性过滤对象数组

[英]Javascript filter an array of objects by a property of object

I have an array of userList which include user basic information. 我有一个userList数组,其中包含用户基本信息。

this.state = {
    userList: [
        { name:"Ann", number:123456789 },
        { name:"Cathy", number:123456789 },
        { name:"Peter", number:123456789 },
        { name:"Ben", number:123456789 },
    ],
    vips: [ 
        { username:"Ann", years:2018 },
        { username:"Peter", years:2019 },
};

How I can return of the vips from userList ? 我如何从userList返回vips vips.username equals to userList.name vips.username等于userList.name

vips_return: [
        { name:"Ann", number:123456789 },
        { name:"Peter", number:123456789 },
]

I tried using .filter and .includes but I am not sure how to deal with objects. 我尝试使用.filter和.includes,但是我不确定如何处理对象。

const vips_return = userList.filter((user)=>
    vips.includes(user.name)
)

You can use find or findIndex : 您可以使用findfindIndex

userList.filter(user => vips.find(vip => vip.username === user.name));

find returns undefined if the value cannot be found in the array, so those are filtered out. 如果在数组中找不到该值,则find返回undefined ,因此将其过滤掉。

If you do this a lot and/or have a large data set, you should build a set of names first: 如果您经常这样做和/或拥有大量数据集,则应首先构建一组名称:

const vipNames = new Set(vips.map(vip => vip.username));

userList.filter(user => vipNames.has(user.name));

You can use a reduce and get the users in userList which are also in vips 您可以使用reduce并在也有vips userList获取用户

 const input = { userList: [ { name:"Ann", number:123456789 }, { name:"Cathy", number:123456789 }, { name:"Peter", number:123456789 }, { name:"Ben", number:123456789 }, ], vips: [ { username:"Ann", years:2018 }, { username:"Peter", years:2019 }, ] }; console.log(input.userList.reduce((acc, val) => { if(!!input.vips.find(vip => vip.username === val.name)) { acc.push(val); } return acc; }, [])); 

Considering that the length of vips should be lower than the one of userList , or at max the same ( vips should be a subset of userList ), maybe a better approach is to map over the vips and get back the corresponding user, like below: 考虑到vips的长度应小于userList的长度,或最大等于vips的长度( vips应该是userList的子集),也许更好的方法是映射在vips上并找回相应的用户,如下所示:

 const input = { userList: [ { name:"Ann", number:123456789 }, { name:"Cathy", number:123456789 }, { name:"Peter", number:123456789 }, { name:"Ben", number:123456789 }, ], vips: [ { username:"Ann", years:2018 }, { username:"Peter", years:2019 }, ] }; console.log(input.vips.map(vip => input.userList.find(user => user.name === vip.username))); 

You could take a Map and get the wanted objects. 您可以获取Map并获取所需的对象。

 var state = { userList: [{ name: "Ann", number: 123456789 }, { name: "Cathy", number: 123456789 }, { name: "Peter", number: 123456789 }, { name: "Ben", number: 123456789 }], vips: [{ username: "Ann", years: 2018 }, { username: "Peter", years: 2019 }] }, user = new Map(state.userList.map(o => [o.name, o])), vips = state.vips.map(({ username }) => user.get(username)); console.log(vips); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Or take a shorter approach with a double mapping 或者采用较短的方法进行双重映射

 var state = { userList: [{ name: "Ann", number: 123456789 }, { name: "Cathy", number: 123456789 }, { name: "Peter", number: 123456789 }, { name: "Ben", number: 123456789 }], vips: [{ username: "Ann", years: 2018 }, { username: "Peter", years: 2019 }] }, vips = state.vips .map(({ username }) => username) .map(Map.prototype.get, new Map(state.userList.map(o => [o.name, o]))); console.log(vips); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暂无
暂无

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

相关问题 Javascript 过滤数组中的对象并返回数组中 Object 中 Object 的属性 - Javascript Filter Objects in Array and return Property of Object in Array of Object in Array 如何通过 Javascript 中的过滤器 object 过滤对象数组? - How to filter in an array of objects by filter object in Javascript? 通过迭代对象内的数组中包含的对象内的属性过滤对象数组[Javascript] - Filter an array of objects by the property inside an object contained in an array inside the object iterated [Javascript] Javascript - 按属性过滤和/或分组对象数组 - Javascript - Filter and/or Group Array of Objects by property 通过 object.property 将数组过滤为唯一对象 - Filter array to unique objects by object.property 如何在VueJS中按对象属性过滤对象数组 - How to filter array of objects by object property in VueJS 过滤对象数组,其中对象属性为日期 - Filter array of objects where object property is date JavaScript:针对特定类型的鞋子过滤鞋子对象数组并返回索引作为属性的对象数组 - JavaScript: Filter Array of Shoe Objects for Specific Type of Shoes and Return Object Array with Index as Property 如何过滤对象数组并检查数组内是否有多个 object 在 Javascript 中具有相同的属性值? - How to filter array of objects and check if more than one object inside the array has the same property value in Javascript? JavaScript根据另一个对象数组的属性过滤对象数组 - JavaScript Filter array of objects based on property of another array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM