Skip to content Skip to sidebar Skip to footer

Lodash - Merge Objects Of Two Arrays On Condition

I have two arrays of objects. arr1 = [ { myName: 'Adam', mySkill: 'CSS', }, { myName: 'Mutalib', mySkill: 'JavaScript', }, ]; arr2 = [ { myName: 'Ad

Solution 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);
<scriptsrc="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);
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

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

Solution 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; }

Solution 4:

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

functionmergeObjectsIfExists(p,a,b){
  var m = a.reduce((m,o) => m.set(o[p],o), newMap());
  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);

Solution 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' } ]

Post a Comment for "Lodash - Merge Objects Of Two Arrays On Condition"