4

I have an array of objects in Javascript:

var List = [
            {
                employee:'Joe',
                type:'holiday',
            },
            {
                employee:'Jerry',
                type:'seminar',

            },
            {
                employee:'Joe',
                type:'shore leave',
            }
           ];

I would like to obtain two new arrays of objects; one for the key employee "Joe" and the other for the key employee "Jerry". The objects should keep the same pairs of key/values.

I have been trying to get a solution using underscore.js, but it is getting too complicated. Any ideas on how this can be achieved?

4
  • 4
    Do you want to provide the names or do you want to group the object automatically by name? Please post what you have tried so far. Commented Mar 7, 2013 at 15:11
  • _.groupBy and _.pluck, most likely. Commented Mar 7, 2013 at 15:13
  • Extract object attribute from list of objects in Javascript Commented Mar 7, 2013 at 15:15
  • There are 2 "Joe"s. What do you want the result to look like? Commented Mar 7, 2013 at 15:19

4 Answers 4

6
var joe = List.filter(function(el){
 return el.employee === "Joe"
});

var jerry = List.filter(function(el){
 return el.employee === "Jerry"
});

This uses Array.prototype.filter and will work in IE9 and up + all recent Chrome/Firefox/Safari/Opera releases.

If you don't know the names in advance then you can create a map var names = {};

for(var i =0; i<List.length; i++){
  var ename = List[i].employee;
  if(typeof names[ename] === "undefined"){
     names[ename] = List.filter(function(el){
     return el.employee === "ename"
    });
   }

}

As a side note, Javascript convention is to only capitalize the first letter of a variable for constructors. So List should probably be list.

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

Comments

4
var emps = {};  
_.each(List, function(item){
   emps[item.employee] = emps[item.employee] || [];
   emps[item.employee].push(item);
});

or using groupBy

var emps = _.groupBy(List, function(item){
   return item.employee;
});

console.log(emps); gives

{
    "Jerry": [
        {
            "employee": "Jerry",
            "type": "seminar"
        }
    ],
    "Joe": [
        {
            "employee": "Joe",
            "type": "holiday"
        },
        {
            "employee": "Joe",
            "type": "shore leave"
        }
    ]
}

Comments

0

Sorry - I don't have the rep. to comment yet but I believe it should be

return el.employee === ename;  // No quotes around ename

Otherwise the answer @Ben gives is perfect - it can be extended into a 'groupby' function if using underscore is out of the question a the project.

Comments

0

For completeness, there is also Underscore's version of _.filter which works with the _.matcher iteratee shorthand. This is essentially a much shorter version of Ben McCormick's answer.

var joe = _.filter(List, {employee: 'Joe'});
var jerry = _.filter(List, {employee: 'Jerry'});

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.