Lodash - Merge objects of two arrays on condition

大城市里の小女人 提交于 2019-12-10 16:33:49

问题


I have two arrays of objects.

arr1 = [
  {
    myName: 'Adam',
    mySkill: 'CSS',
  },
  {
    myName: 'Mutalib',
    mySkill: 'JavaScript',
  },
];

arr2 = [
  {
    myName: 'Adam',
    myWeight: '112',
  },
  {
    myName: 'Habib',
    myWeight: '221',
  },
];

The result I want is an array that contains objects of first array that have a matching property "myName" in second array, with the additional properties of the corresponding second array object.

result = [
  {
    myName = 'Adam'
    mySkill = 'CSS'
    myWeight = '112'
  }
];

回答1:


The solution below groups the concatenated array (arr1 and arr2) by myName, removes all groups that only contains one item using reject, and lastly use map to merge the resulting array.

var result = _(arr1)
  .concat(arr2)
  .groupBy('myName')
  .reject({ length: 1 })
  .map(_.spread(_.merge))
  .value();

var arr1 = [
  {
    myName: 'Adam',
    mySkill: 'CSS',
  },
  {
    myName: 'Mutalib',
    mySkill: 'JavaScript',
  }
];

var arr2 = [
  {
    myName: 'Adam',
    myWeight: '112',
  },
  {
    myName: 'Habib',
    myWeight: '221',
  }
];

var result = _(arr1)
  .concat(arr2)
  .groupBy('myName')
  .reject({ length: 1 })
  .map(_.spread(_.merge))
  .value();

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

An alternative solution is to use intersectionWith to get the intersection between two arrays and assign the missing values at the same time. Note the use of cloneDeep to promote immutability.

var result = _.intersectionWith(_.cloneDeep(arr1), arr2, function(x, y) {
  return x.myName === y.myName && _.assign(x, y);
});

var arr1 = [
  {
    myName: 'Adam',
    mySkill: 'CSS',
  },
  {
    myName: 'Mutalib',
    mySkill: 'JavaScript',
  }
];

var arr2 = [
  {
    myName: 'Adam',
    myWeight: '112',
  },
  {
    myName: 'Habib',
    myWeight: '221',
  }
];

var result = _.intersectionWith(_.cloneDeep(arr1), arr2, function(x, y) {
  return x.myName === y.myName && _.assign(x, y);
});
  
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>



回答2:


Alternative native JS solution using Array.prototype.forEach and Object.assign functions:

var arr1 = [
    { myName: 'Adam', mySkill: 'CSS'}, { myName: 'Mutalib', mySkill: 'JavaScript'},
  ],

  arr2 = [
    { myName: 'Adam', myWeight: '112'}, { myName: 'Habib', myWeight: '221'}
  ],
  result = [];
  
arr1.forEach(function (o) {
    arr2.forEach(function (c) {
        if (o.myName === c.myName) result.push(Object.assign({}, o, c));
    })
});

console.log(result);



回答3:


You could use a hash table and push the object only if myName is common.

var arr1 = [{ myName: 'Adam', mySkill: 'CSS' }, { myName: 'Mutalib', mySkill: 'JavaScript' }],
    arr2 = [{ myName: 'Adam', myWeight: '112' }, { myName: 'Habib', myWeight: '221' }],
    hash = Object.create(null),
    result = [];

arr1.forEach(function (a) {
    hash[a.myName] = { myName: a.myName, mySkill: a.mySkill };
});

arr2.forEach(function (a) {
    if (hash[a.myName]) {
        hash[a.myName].myWeight = a.myWeight;
        result.push(hash[a.myName]);
    }
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }



回答4:


You may also do as follows with the help of a Map object.

function mergeObjectsIfExists(p,a,b){
  var m = a.reduce((m,o) => m.set(o[p],o), new Map());
  return b.reduce((r,o) => m.has(o[p]) ? (r.push(m.set(o[p],Object.assign(m.get(o[p]),o)).get(o[p])),r)
                                       : r, []);
}

var arr1 = [{ myName: 'Adam', mySkill: 'CSS'}, { myName: 'Mutalib', mySkill: 'JavaScript'}, { myName: 'Jessuro', mySkill: 'Haskell'}],
    arr2 = [{ myName: 'Adam', myWeight: '112'}, { myName: 'Habib', myWeight: '221'}, { myName: 'Jessuro', myIQ: '150'}],
  result = mergeObjectsIfExists("myName",arr1,arr2);

console.log(result);



回答5:


If using lodash/fp you could compose functions to achieve what you want. Assuming myName is your PK (or merge pivot), you could do:

const mergeByName = compose(values, spread(merge), map(keyBy('myName')));

> mergeByName([arr1, arr2])
[ { myName: 'Adam', mySkill: 'CSS', myWeight: '112' },
  { myName: 'Mutalib', mySkill: 'JavaScript' },
  { myName: 'Habib', myWeight: '221' } ]


来源:https://stackoverflow.com/questions/42738285/lodash-merge-objects-of-two-arrays-on-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!