0

I have a JavaScript object

I'm trying to group these objects by id, and produce a map of a list of names where the key is the id, and the value is the name. Example:

{1 : [John, Jane, Mike], 3: [Bob, Will]}

With the code below, I get:

{1: [John, Jane, Mike, Bob, Will], 3: [John, Jane, Mike, Bob, Will]}

How can I achieve my desired output? Any help would be appreciated.

var myObj = [{
    id: 1,
    name: "John",
    major: "accounting"
  },
  {
    id: 1,
    name: "Jane",
    major: "accounting"
  }, {
    id: 1,
    name: "Mike",
    major: "accounting"
  }, {
    id: 3,
    name: "Bob",
    major: "history"
  }, {
    id: 3,
    name: "Will",
    major: "history"
  }]



for (i in myObj) {
  var obj = myObj[i];
  var id = obj.id;
  var name = obj.name;
  var newMap = {};
  var namesArray = [];

  if (!(id in newMap)) {
    namesArray.push(name);
    newMap[id] = namesArray;

  } else {
    for (key in newMap) {
      if (key == id) {
        namesArray.push(name);
        newMap[key] = namesArray;
      }
    }
  }
  console.log(newMap);
}

2
  • I tried to create a snippet for you. The array was not a working solution and it does not need for (i in myObj) . Please fix the code and the object to reflect your comments Commented Feb 18, 2017 at 18:05
  • Thank you everyone for the solutions Commented Feb 18, 2017 at 18:18

5 Answers 5

1

Editing your own program:

var myObj = [{
    id: 1,
    name: "John",
    major: "accounting"
  },
  {
    id: 1,
    name: "Jane",
    major: "accounting"
  }, {
    id: 1,
    name: "Mike",
    major: "accounting"
  }, {
    id: 3,
    name: "Bob",
    major: "history"
  }, {
    id: 3,
    name: "Will",
    major: "history"
  }]


  var newMap = {};
  var namesArray = [];
     
for (i in myObj) {
  var obj = myObj[i];
  var id = obj.id;
  var name = obj.name;

  if (!(id in newMap)) {
     namesArray = [];
     namesArray.push(name);
    newMap[id] = namesArray;

  } else {
        namesArray = newMap[id];
        namesArray.push(name);
     
  }
  
}
console.log(newMap);

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

1 Comment

@developer-x, please have a look at my answer too.
0
var myObj = [{id: 1, name: "John", major:"accounting"},
            {id: 1, name: "Jane", major:"accounting"},
            {id: 1, name: "Mike", major:"accounting"},
            {id: 3, name: "Bob",  major:"history"},
            {id: 3, name: "Will", major:"history"}];

var group = {};

for(var i = 0; i < myObj.length; i++){
  if(myObj[i].id in group){
    group[myObj[i].id].push(myObj[i].name);
  } else {
    group[myObj[i].id] = [];
    group[myObj[i].id].push(myObj[i].name);
  }
}
console.log(group);

Comments

0

Another solution using map function.

var objs = [{id: 1, name: "John", major:"accounting"},
            {id: 1, name: "Jane", major:"accounting"},
            {id: 1, name: "Mike", major:"accounting"},
            {id: 3, name: "Bob",  major:"history"},
            {id: 3, name: "Will", major:"history"}];

var namesForId = [], currentId = null, theObj = {};
objs.map((item) => {
    if (item.id === currentId) {
       namesForId.push(item.name);
    } else {
       currentId = item.id; 
       namesForId = [item.name];
       theObj[item.id] = namesForId;
    }    
})
console.log(theObj);

Comments

0

Another method that uses Array#reduce but a bit more compact.

const input = [{ id: 1, name: "John", major: "accounting" }, { id: 1, name: "Jane", major: "accounting" }, { id: 1, name: "Mike", major: "accounting" }, { id: 3, name: "Bob", major: "history" }, { id: 3, name: "Will", major: "history" } ];

console.log(input.reduce((m, e) => ((m[e.id] = m[e.id] || []).push(e.name), m), {}))

Comments

0

Using Array.prototype.reduce().

var myObj = [{id: 1, name: "John", major:"accounting"},
             {id: 1, name: "Jane", major:"accounting"},
             {id: 1, name: "Mike", major:"accounting"},
             {id: 3, name: "Bob",  major:"history"   },
             {id: 3, name: "Will", major:"history"   }];

var result = myObj.reduce(function(acc, item) {
  acc[item.id] = acc[item.id] || [];
  acc[item.id].push(item.name);
  return acc;
}, {});

console.log(result);

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.