I have array and I want to merge duplicated items.
var arr = [{
'id': 1,
'text': 'ab'
}, {
'id': 1,
'text': 'cd'
}, {
'id': 2,
'text': 'other'
}, {
'id': 3,
'text': 'afafas'
}, {
'id': 4,
'text': 'asfasfa'
}];
var work = arr.reduce(function(p, c) {
var key = c.id;
p[key].text.push(c.text);
});
console.log(work);
And output must be like that:
[{
'id': 1,
'text': ["[ab] [cd]"]
}, {
'id': 2,
'text': 'other'
}, {
'id': 3,
'text': 'afafas'
}, {
'id': 4,
'text': 'asfasfa'
}]
Here is what I tried but result is fail: ( https://jsfiddle.net/2m7kzkba/