0

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]);
        }
    }
Houla Banada
  • 77
  • 1
  • 6

2 Answers2

1

Do you need to remove duplicates?

var arr = [
    {type: "Fruit", items:[{word:"Apple"}]}, 
    {type: "Fruit", items:[{word:"Orange"}]}
];

var obj = arr.reduce(function(obj, i) {
    obj[i.type] = [].concat(obj[i.type] || [], i.items); return obj;    
}, {});

var res = [];
for (var key in obj)
    res.push({type: key, items: obj[key]});
    // or to unique items
    // res.push({type: key, items: obj[key].filter(function (e, i, r) { return r.indexOf(e) == i})}); 

console.log(res);
Aikon Mogwai
  • 4,954
  • 2
  • 18
  • 31
1

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

Niles Tanner
  • 3,911
  • 2
  • 17
  • 29