是否有更短的方法来合并重复的对象

时间:2016-08-26 23:42:49

标签: javascript node.js

我很好奇是否有更短的方法来实现这个目标?

通过匹配某些属性来检查是否存在重复项,然后将重复的array.items推送到原始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)将成为

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

2 个答案:

答案 0 :(得分:1)

您需要删除重复项吗?

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

答案 1 :(得分:1)

你可以使用一个对象帮助排序,这里我对类型进行排序并组合项目

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

工作示例:http://codepen.io/nilestanner/pen/NAkZbz