简体   繁体   English

使用键数组过滤对象数组并提取其属性

[英]Filter an array of objects and extract its property by using an array of keys

im having this problem which i cant wrap around my head, 我有这个问题,我不能缠住我的头,

much better if put in a code. 如果输入代码,会更好。

//Array of objects sample
var objects = [{id:1, name:'test1'}, {id:2, name:'test2'}, {id:3, name: 'test3'}, {id:4, name:'test4'}];
var arrayOfKeys = [3,1,4];

//extract object name property if its id property is equivalent to one of arrayOfKeys [3,1] 
//var arrayOfKeys = [3,1,4];
//output sample: extractedName=['test3','test1','test4'];

i've been using filter and map but no avail, also tried nesting filter inside map im getting an arrays of array and inside is a single object. 我一直在使用过滤器和地图,但无济于事,还尝试在地图内嵌套过滤器,以获取数组数组,并且内部是单个对象。

You could map the objects and ren map the wanted keys for getting the name. 您可以映射对象,也可以映射想要的键以获得名称。

 var objects = [{ id: 1, name: 'test1' }, { id: 2, name: 'test2' }, { id: 3, name: 'test3' }], arrayOfKeys = [3, 1], result = arrayOfKeys.map((map => id => map.get(id).name)(new Map(objects.map(o => [o.id, o])))); console.log(result); 

I assume you need to map array numbers to id properties? 我假设您需要将数组编号映射到id属性? Here's the code where you map through numbers and find inside your array to handle situations when there's no such id in objects array: 这是代码,您find在其中map数字并在数组内部find以处理对象数组中没有此类id情况:

 var objects = [{id:1, name:'test1'}, {id:2, name:'test2'}, {id:3, name: 'test3'}]; var arrayOfKeys = [3,1]; var res = arrayOfKeys.map(key => { var found = objects.find(o => o.id == key); return found ? found.name : false; }) console.log(res) 

 let objects = [{id:1, name:'test1'}, {id:2, name:'test2'}, {id:3, name: 'test3'}, {id:4, name:'test4'}], arrayOfKeys = [3,1,4]; let result = objects.reduce((res, obj) => { // loop over the array of objects let index = arrayOfKeys.indexOf(obj.id); // check if the current object's id is in the array of keys if(index !== -1) { // if it's there res.push(obj.name); // then add the current object's name to the result array arrayOfKeys.splice(index, 1); // remove its id from the array of keys so we won't search for it again (optional, slightly better than leaving it there) } return res; }, []); console.log(result); 

I think you were on the right track with the filter. 我认为您在使用筛选器的过程中处于正确的轨道。 You can make it readable and concise with some. 您可以使它变得可读和简洁。

 var objects = [{id: 1,name: 'test1'}, {id: 2,name: 'test2'}, {id: 3,name: 'test3'}, {id: 4,name: 'test4'}], arrayOfKeys = [3, 1, 4]; var result = objects.filter((x, i) => { if (arrayOfKeys.some(k => x.id === k)) { return true; } }) console.log(result.map(x=>x.name)); 

Something even shorter! 更短的东西! would be 将会

 var objects = [{id: 1,name: 'test1'}, {id: 2,name: 'test2'}, {id: 3,name: 'test3'}, {id: 4,name: 'test4'}],arrayOfKeys = [3, 1, 4]; var result = objects.filter((x, i) => arrayOfKeys.some(k => x.id === k)); console.log(result.map(x=>x.name)); 

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

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