0

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.

1
  • 1
    I feel like making a new object, using the person's name as the key to create your jobs would be easier Commented Jun 22, 2017 at 1:52

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

Never thought of the indexOf method, thank you dude i learned something, so if '--1' it simply means it doesn't exist on the array right?
Yeah exactly. If indexOf returns -1 then it doesn't exist in the array. It's a really useful function.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.