Houla Banada Houla Banada - 3 months ago 8
Javascript Question

Is there a shorter way to merge duplicated objects

I was curious if there was a shorter way to accomplish this?

Check if there are duplicates by matching certain properties then push the duplicated array.items into the original array.items

var array = [
{type: "Fruit", something: "abc", more: "properties", items: [{word:"Apple", abc: "ok"}]},
{type: "Fruit", something: "abc", more: "properties", items: [{word:"Orange", abc: "ok"}]}
];


merge(array) would become

array = [
{type: "Fruit", something: "abc", more: "properties", items:[{word:"Apple", abc: "ok"}, {word:"Orange", abc: "ok"}]}
];

merge: function(arr) {
var rec, dupeIndices, foundDupes;
for (var idx = 0, len = arr.length; idx < len - 1; ++idx) {
rec = arr[idx];
if (rec === null) continue;
dupeIndices = findDupeIndices(rec, arr.slice(idx + 1), idx + 1);
if (dupeIndices.length === 0) continue;
foundDupes = true;
processDupes(rec, dupeIndices, arr);
}
if (foundDupes) cleanUp(arr);
},
cleanUp: function(arr) {
for (var idx = 0; idx < arr.length; ++idx) {
if (arr[idx] === null) arr.splice(idx--, 1);
}
},
processDupes: function(rec, dupeIndices, arr) {
var dupeRealIdx, dupeRec;
for (var dupeIdx = 0, dupesLen = dupeIndices.length; dupeIdx < dupesLen; ++dupeIdx) {
dupeRealIdx = dupeIndices[dupeIdx];
dupeRec = arr[dupeRealIdx];
updateRecord(rec, dupeRec);
arr[dupeRealIdx] = null;
}
},
findDupeIndices: function(rec, arr, offset) {
var other, result = [];
for (var idx = 0, len = arr.length; idx < len; ++idx) {
other = arr[idx];
if (other === null) continue;
if (isDupe(rec, other)) result.push(idx + offset);
}
return result;
},
isDupe: function(a, b) {
return (a.type === b.type && a.something === b.something);
},
updateRecord: function(rec, dupe) {
for (var i = 0; i < dupe.items.length; i++) {
rec.items.push(dupe.items[i]);
}
}

Answer

You can use an object to help sort, here im sorting on the type and combining the items

 var obj = [
    {type: "Fruit", something: "abc", more: "properties", items: [{word:"Apple", abc: "ok"}]},
    {type: "Fruit", something: "abc", more: "properties", items: [{word:"Orange", abc: "ok"}]}
];

var sorted = {};
obj.forEach(function(element){
  if(sorted[element.type]){
    sorted[element.type].items.push.apply(sorted[element.type].items,element.items)
  }else{
    sorted[element.type] = {
      type: element.type,
      something: element.something,
      more: element.more,
      items:element.items
    }
  }

});

var sortedarray = Object.keys(sorted).map(function(item){
return sorted[item]});

console.log(sortedarray);

working example : http://codepen.io/nilestanner/pen/NAkZbz