【问题标题】:lodash merge and combine objectslodash合并和组合对象
【发布时间】:2016-06-12 08:36:52
【问题描述】:

我有一个对象数组,如下所示,我使用 sequelize ORM 从我的数据库中读取: 我想从一个部分中获取我的所有视频,但是使用 sequelize 可以返回的更好的是:

[{
    "id": 2,
    "name": "Ru",
    "subsection": 1,
    "Video": {
      "id": 11,
      "source": "sourrrccrsss22222",
      "videoSubSection": 2
    }
  },
  {
    "id": 2,
    "name": "Ru",
    "subsection": 1,
    "Video": {
      "id": 12,
      "source": "sourrrccrsss111",
      "videoSubSection": 2
    }
  },
  {
    "id": 1,
    "name": "Oc",
    "subsection": 1,
    "Video": {
      "id": 13,
      "source": "sourrrcc",
      "videoSubSection": 1
    }
  },
  {
    "id": 1,
    "name": "Oc",
    "subsection": 1,
    "Video": {
      "id": 14,
      "source": "sourrrcc",
      "videoSubSection": 1
    }
  }]

有没有办法合并和组合我数组中的对象以获得类似的东西:

[{
    "id": 2,
    "name": "Ru",
    "subsection": 1,
    "Video": [{
      "id": 11,
      "source": "sourrrccrsss22222",
      "videoSubSection": 2
    },{
      "id": 12,
      "source": "sourrrccrsss111",
      "videoSubSection": 2
    }]
  },
  {
    "id": 1,
    "name": "Oc",
    "subsection": 1,
    "Video": [{
      "id": 13,
      "source": "sourrrcc",
      "videoSubSection": 1
    },{
      "id": 14,
      "source": "sourrrcc",
      "videoSubSection": 1
    }]
  }

最接近的函数是 _.mergeWith(object, sources, customizer) 但我遇到的主要问题是我有对象并且需要合并这个对象。

【问题讨论】:

    标签: javascript sequelize.js lodash


    【解决方案1】:

    在纯 Javascript 中,您可以将 Array#forEach() 与数组的临时对象一起使用。

    var data = [{ id: 2, name: "Ru", subsection: 1, Video: { id: 11, source: "sourrrccrsss22222", VideoSubSection: 2 } }, { id: 2, name: "Ru", subsection: 1, Video: { id: 12, source: "sourrrccrsss111", VideoSubSection: 2 } }, { id: 1, name: "Oc", subsection: 1, Video: { id: 13, source: "sourrrcc", VideoSubSection: 1 } }, { id: 1, name: "Oc", subsection: 1, Video: { id: 14, source: "sourrrcc", VideoSubSection: 1 } }],
        merged = function (data) {
            var r = [], o = {};
            data.forEach(function (a) {
                if (!(a.id in o)) {
                    o[a.id] = [];
                    r.push({ id: a.id, name: a.name, subsection: a.subsection, Video: o[a.id] });
                }
                o[a.id].push(a.Video);
            });
            return r;
        }(data);
    
    document.write('<pre>' + JSON.stringify(merged, 0, 4) + '</pre>');

    【讨论】:

      【解决方案2】:

      也许试试transform():

      _.transform(data, (result, item) => {
        let found;
      
        if ((found = _.find(result, { id: item.id }))) { 
          found.Video.push(item.Video);
        } else {
          result.push(_.defaults({ Video: [ item.Video ] }, item));
        }
      }, []);
      

      在这里使用reduce() 也可以,但transform() 不那么冗长。

      【讨论】:

      • 我会尽量尝试,解决方案很优雅,几行代码。
      • 完美工作!
      【解决方案3】:

      您可以这样做(test 是您的数据库输出)

      var result = [];
      var map = [];
      
      _.forEach(test, (o) => {
        var temp = _.clone(o);
        delete o.Video;
        if (!_.some(map, o)) {
          result.push(_.extend(o, {Video: [temp.Video]}));
          map.push(o);
        } else {
          var index = _.findIndex(map, o);
          result[index].Video.push(temp.Video);
        }
      });
      
      console.log(result); // outputs what you want.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-04
        • 2017-02-22
        • 1970-01-01
        • 2018-08-07
        • 2019-05-07
        • 2015-07-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多