【问题标题】:Merging JS array of objects合并JS对象数组
【发布时间】:2017-07-25 11:35:29
【问题描述】:

好的,所以我有两个要合并的对象,但想知道最好的方法是什么...

obj1 = [
         { 
           id: 123, 
           list: [ {id:1}, {id:2}, {id: 3} ]
         },
         {
           id: 456
           list: [ {id:99}, {id:98}, {id: 97} ]
         }
       ]


obj1 = [
         { 
           id: 123, 
           list: [ {id:1}, {id:4}, {id: 5} ]
         },
         {
           id: 456
           list: [ {id:99}, {id:100}, {id: 101} ]
         }
       ]

我希望能够调用merge(obj1, obj2) 之类的东西,结果看起来像:

resultObj = [
         { 
           id: 123, 
           list: [ {id:1}, {id:2}, {id:3}, {id:4}, {id: 5} ]
         },
         {
           id: 456
           list: [ {id:99}, {id:98}, {id:97}, {id:100}, {id: 101} ]
         }
       ]

【问题讨论】:

  • 那么,你尝试了什么?
  • 你已经尝试过了吗?这实际上只是使用一些非常标准的循环或数组操作的问题,例如Array#forEachArray#filter 等......
  • @t.niese 不,这不是一回事。
  • @CengizAraz 我在你写评论的同时删除了重复的投票。
  • 我已经设法以使用循环的标准方式做到这一点,但认为可能有一种新方式(es6)但看起来不像

标签: javascript object merge


【解决方案1】:

您可以使用 forEach() 在第一级按 id 对元素进行分组,然后再使用一个 forEach()find() 来检查列表中的对象并推送到数组或分配给具有相同 id 的现有对象。

var obj1 = [{"id":123,"list":[{"id":1},{"id":2},{"id":3}]},{"id":456,"list":[{"id":99},{"id":98},{"id":97}]}]
var obj2 = [{"id":123,"list":[{"id":1},{"id":4},{"id":5}]},{"id":456,"list":[{"id":99},{"id":100},{"id":101}]}]  
       
var result = []       
var arr = [].concat(obj1, obj2)
arr.forEach(function(e) {
  if(!this[e.id]) result.push(this[e.id] = e)
  else {
    var that = this;
    e.list.forEach(function(a) {
      var o = that[e.id].list.find(c => a.id == c.id);
      o ? Object.assign(o, a) : that[e.id].list.push(a)
    })
  }
}, {})    
     
console.log(result)

【讨论】:

    【解决方案2】:

    您可以为相同的id 属性使用嵌套哈希表。此解决方案复制对象中的所有属性。

    function merge(source, target, hash) {
        source.forEach(function (o) {
            if (!hash[o.id]) {
                hash[o.id] = { _: Object.create(null), data: { id: o.id } };
                target.push(hash[o.id].data);
            }
            Object.keys(o).forEach(function (k) {
                if (['id', 'list'].indexOf(k) !== -1) {
                    return;
                }
                hash[o.id].data[k] = o[k];
            });
            if (o.list) {
                hash[o.id].data.list = hash[o.id].data.list || [];
                merge(o.list, hash[o.id].data.list, hash[o.id]._);
            }
        });
    }
    
    var array1 = [{ id: 123, list: [{ id: 1, a: 3 }, { id: 2 }, { id: 3 }] }, { id: 456, list: [{ id: 99 }, { id: 98 }, { id: 97 }] }],
        array2 = [{ id: 123, list: [{ id: 1, b: 4 }, { id: 4 }, { id: 5 }] }, { id: 456, list: [{ id: 99 }, { id: 100 }, { id: 101 }] }],
        hash = Object.create(null),
        result = [];
    
    [array1, array2].forEach(function (a) {
        merge(a, result, hash);
    });
       
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      猜你喜欢
      • 2019-09-04
      • 1970-01-01
      • 2022-11-23
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多