基于公共密钥合并/连接两个对象(push?splice?)

时间:2016-10-01 13:30:17

标签: javascript arrays object merge lodash

我一直在尝试使用pushsplice等,但没有成功。我希望你能给我一些线索(或两个)让它发挥作用。在我的问题下面,这似乎并不那么难...... :(

我有两个对象:

对象#1:

houses:      [ { _id: 1,
                 KLLS: '72797-194155',
                 date : '01/01/1984'},

               { _id: 2,
                 KLLS: '84773-949399',
                 date : '01/01/1984'}
             ]

对象#2:

works:       [ { _id: 27,
                 KLLS: '72797-194155',
                 stuff : 'some stuff'},

               { _id: 28,
                 KLLS: '72797-194155', // Note that KLLS key is the same as id:27
                 stuff : 'some stuff'},

               { _id: 29,
                 KLLS: '84773-949399',
                 stuff : 'some stuff'},

               { _id: 30,
                 KLLS: '84773-949399', // Note that KLLS key is the same as id:29
                 stuff : 'some stuff'},

]

我想要实现的是这样的:

[ { _id: 1,
    KLLS: '72797-194155',
    date : '01/01/1984',
    stuff: 
         [ { _id: 27,
             KLLS: '72797-194155',
             stuff : 'some stuff'},

           { _id: 28,
             KLLS: '72797-194155',
             stuff : 'some stuff'}
         ]
   },

  { _id: 2,
    KLLS: '84773-949399',
    date : '01/01/1984',
    stuff: 
         [ { _id: 27,
             KLLS: '72797-194155',
             stuff : 'some stuff'},

           { _id: 28,
             KLLS: '72797-194155',
             stuff : 'some stuff'}
         ]}
]

实际上我现在正在使用Lodash:

for(var i = 0; i < houses.length; i++) {

      // get the KLLS key for each house[i]
      var klls = houses[i].KLLS

     // retrieve the works corresponding to KLLS key for house[i]
     var trvx = _.filter(works, ['KLLS', klls])

    // Merge the two to get the wanted output...
    --> Whatever I try here, nothing works...
        even using splice or push...

}

你有什么线索让我走上正轨吗?

3 个答案:

答案 0 :(得分:0)

你可以这样做......

var a = [{
    _id: 1,
    KLLS: '72797-194155',
    date: '01/01/1984'
  },

  {
    _id: 2,
    KLLS: '84773-949399',
    date: '01/01/1984'
  }
]
var b = [{
    _id: 27,
    KLLS: '72797-194155',
    stuff: 'some stuff'
  },

  {
    _id: 28,
    KLLS: '72797-194155', // Note that KLLS key is the same as id:27
    stuff: 'some stuff'
  },

  {
    _id: 29,
    KLLS: '84773-949399',
    stuff: 'some stuff'
  },

  {
    _id: 30,
    KLLS: '84773-949399', // Note that KLLS key is the same as id:29
    stuff: 'some stuff'
  },

]
var newobj = []

var n = a.map(function(el) {
  var temp = []
  b.map(function(el2) {
    if (el.KLLS === el2.KLLS) {
      temp.push(el2)
    }
  })
  el.stuffs = temp
  newobj.push(el)
})

console.log(newobj)

答案 1 :(得分:0)

很遗憾有lodash并没有使用任何方便的方法来解决问题。

houses标记为'house'的JSON和works标记为'works'的JSON,您可以执行以下操作来获得解决方案:

var res = _.reduce(houses, function (memo, house) {
    return memo.concat(_.extend({}, house, {
        stuff: _.filter(works, {KLLS :house.KLLS})
    }));
}, []);

console.log(res);

我们的想法是reducehouses数组添加到一系列新的房屋extended中,并完成工作。

我更喜欢创建一个新对象而不是修改原始对象,以便该函数尽可能为pure

答案 2 :(得分:0)

Concat数组,按KLLS分组,然后减少每个组数组以生成所需的对象:

function combine(arr1, arr2) {
  return _(arr1)
    .concat(arr2) // concat the 2nd array
    .groupBy('KLLS') // group the items by KLLS
    .map(function(group) {
      return group.reduce(function(result, obj) {
        if (obj.date) { // if it's an object with date, merge it with current result and return
          return _.merge(result, obj);
        }

        result.stuff.push(obj);
        return result;
      }, { stuff: [] }); // initial value is an object with stuff array
    })
    .value(); // end the chain
}

var houses = [{
  _id: 1,
  KLLS: '72797-194155',
  date: '01/01/1984'
}, {
  _id: 2,
  KLLS: '84773-949399',
  date: '01/01/1984'
}];

var works = [{
    _id: 27,
    KLLS: '72797-194155',
    stuff: 'some stuff'
  },

  {
    _id: 28,
    KLLS: '72797-194155',
    stuff: 'some stuff'
  },

  {
    _id: 29,
    KLLS: '84773-949399',
    stuff: 'some stuff'
  },

  {
    _id: 30,
    KLLS: '84773-949399',
    stuff: 'some stuff'
  },
];

var result = combine(houses, works);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.2/lodash.min.js"></script>