简体   繁体   中英

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'.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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