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]);
}
}