简体   繁体   English

过滤对象数组并使用值数组获取其属性

[英]Filter an array of objects and get its property by using an array of values

Hey guys this must be seem repetitive as i already questioned this which is already been answered but this time its a little different. 大家好,这似乎是重复的,因为我已经质疑了这个问题,但已经回答了,但是这次有所不同。

Previous question: My previous question which is already been answered 上一个问题: 我已经回答过的上一个问题

So this is my question, i have an array of objects(people) with property called 'name', and 'role'. 所以这是我的问题,我有一组对象(人),其属性分别为“名称”和“角色”。 I have another array which called 'Jobs'. 我有另一个数组称为“工作”。 Much better if i use a code sample. 如果我使用代码示例,那就更好了。

var jobs = ['engineer','scientist','developer'];
var people = [ {name:'John', role:'engineer'},
               {name:'Jane', role:'scientist'},
               {name:'Jonathan', role:'developer'},
               {name:'Jane', role:'engineer'} ];

As you can see object with same property 'name' can be seen but with different role. 如您所见,可以看到具有相同属性“名称”的对象,但角色不同。 I want to extract them to a new array using the array of 'jobs' base on their role. 我想根据其角色使用“职位”数组将它们提取到新数组中。

Example output will be: 输出示例如下:

var peopleWithJobs = [
                      {name:'John', jobs:['engineer'] }
                      {name:'Jane', jobs:['scientist', 'engineer'] },
                      {name:'Jonathan', jobs:['developer'] } 
                     ]

if 'name' property value is repeated on the array of 'people' just get the role and push/append to jobs property of the new array 'peopleWithJobs'. 如果在'people'数组上重复'name'属性值,则只需获得角色并将其推入/追加到新数组'peopleWithJobs'的jobs属性中即可。

I've been using map and filter higher order functions but im fairly new to javascript and just can't wrap my head around to this logic. 我一直在使用map和filter更高阶的函数,但是对于javascript来说还很陌生,只是无法理解这个逻辑。

 var jobs = ['engineer','scientist','developer']; var people = [ {name:'John', role:'engineer'}, {name:'Jane', role:'scientist'}, {name:'Jonathan', role:'developer'}, {name:'Jane', role:'engineer'} ]; var peopleWithJobs = []; for(var x=0;x<people.length;x++) { if(jobs.indexOf(people[x].role) != -1) { var uniqueNames = peopleWithJobs.map(function(val) { return val.name; }); if(uniqueNames.indexOf(people[x].name) == -1) peopleWithJobs.push({name: people[x].name, jobs: [people[x].role]}); else { peopleWithJobs[uniqueNames.indexOf(people[x].name)].jobs.push(people[x].role); } } } console.log(peopleWithJobs); 

When you first sort the array, constructing the new array is pretty straightforward. 当您第一次对数组进行排序时,构造新数组非常简单。 You can push the items to the new array one by one, after checking if the name is same as previous name, in which case you only push the role to the previous person. 在检查名称是否与以前的名称相同之后,您可以将项目逐个推送到新数组中,在这种情况下,您只能将角色推送到先前的人员。

 var people = [ {name:'John', role:'engineer'}, {name:'Jane', role:'scientist'}, {name:'Jonathan', role:'developer'}, {name:'Jane', role:'engineer'} ]; function reconstruct(arr) { // Sort var arr = arr.slice().sort(function(a,b) { var x = a.name.toLowerCase(); var y = b.name.toLowerCase(); return x < y ? -1 : x > y ? 1 : 0; }); // Construct new array var newArr = []; for (i=0; i<arr.length; i++) { if (arr[i-1] && (arr[i].name == arr[i-1].name)) { newArr[newArr.length-1].role.push(arr[i].role) } else { newArr.push({name: arr[i].name, role: [arr[i].role]}); } } return newArr; } console.log(reconstruct(people)); 

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

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