2

I've a requirement where the below array of objects should be converted to the result mentioned.

list = [{name : 'John', id : '1'}, {name : 'John', id : '2'}, {name : 'John', id : '3'}, {name : 'John', id : '4'}];

result to

list = [{name : 'John', id : '1, 2, 3, 4'}];

How can one do it?

asked Apr 22, 2016 at 7:54
3
  • Do you want the id to be a string or an array? i.e. '1,2,3,4' or ['1','2','3','4']? Commented Apr 22, 2016 at 8:03
  • This is a duplicate of stackoverflow.com/questions/9229645/… Commented Apr 22, 2016 at 8:06
  • It's a string with comma separated. Commented Apr 22, 2016 at 8:08

3 Answers 3

3

You could do it using a temporary object and Array#filter to filter out the duplicates and modify the original at the same time:

var list = [
 {name : 'John', id : '1'},
 {name : 'John', id : '2'},
 {name : 'John', id : '3'},
 {name : 'John', id : '4'}
 ],
 temp = {};
list = list.filter(function (o) {
 if (temp.hasOwnProperty(o.name)) {
 temp[o.name].id += ', ' + o.id;
 return false;
 }
 else {
 temp[o.name] = o;
 return true;
 }
});
document.body.textContent = JSON.stringify(list);

answered Apr 22, 2016 at 8:05
Sign up to request clarification or add additional context in comments.

3 Comments

How is it modifying the original list?
Okay, now I get it. You are working with the o. Thanks!
@PrateekAgarwal: your welcome. Don't forget to accept the answer that works best for you :-).
1

This is to solve your specific question rather than a more general solution

function consolidate(list){
 var names={};
 list.forEach(function(e){
 if (!names[e.name]) names[e.name]=[e.id];
 else names[e.name].push(e.id);
 });
 var rtn=[];
 Object.keys(names).forEach(function(n){
 rtn.push({name:n, id:names[n].join(', ')});
 });
 return rtn;
}

basically use an object to accumulate the data then remap back into an array.

answered Apr 22, 2016 at 8:08

2 Comments

why map? it does not return something.
Good point. This is really just my lazyness in typing, but you are correct and I've changed it to forEach
1

You can group it in a single loop with the help of a temoprary object without mutating the original array.

var list = [{ name: 'John', id: '1' }, { name: 'John', id: '2' }, { name: 'John', id: '3' }, { name: 'John', id: '4' }],
 grouped = [];
list.forEach(function (a) {
 if (!this[a.name]) {
 this[a.name] = { data: { name: a.name }, array: [] };
 grouped.push(this[a.name].data);
 }
 this[a.name].array.push(a.id);
 this[a.name].data.id = this[a.name].array.join(', ');
}, Object.create(null));
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');

A slightly compacter version

var list = [{ name: 'John', id: '1' }, { name: 'John', id: '2' }, { name: 'John', id: '3' }, { name: 'John', id: '4' }],
 grouped = [];
list.forEach(function (a) {
 if (!this[a.name]) {
 this[a.name] = { name: a.name, id: a.id };
 grouped.push(this[a.name]);
 return;
 }
 this[a.name].id += ', ' + a.id;
}, Object.create(null));
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');

answered Apr 22, 2016 at 8:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.