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
Prateek Agarwal
4171 gold badge9 silver badges20 bronze badges
3 Answers 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
Andy E
346k86 gold badges482 silver badges452 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Prateek Agarwal
How is it modifying the original list?
Prateek Agarwal
Okay, now I get it. You are working with the o. Thanks!
Andy E
@PrateekAgarwal: your welcome. Don't forget to accept the answer that works best for you :-).
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
Euan Smith
2,1921 gold badge20 silver badges30 bronze badges
2 Comments
Nina Scholz
why map? it does not return something.
Euan Smith
Good point. This is really just my lazyness in typing, but you are correct and I've changed it to forEach
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
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Comments
lang-js
idto be a string or an array? i.e.'1,2,3,4'or['1','2','3','4']?