Plain JS or Lodash - replace object values with values from other object

Viewed 1466

I have two objects:

let first = {
   a: 'John',
   b: 22,
   c: 'example'
}

let second = {
   b: 55,
   d: 'demo'
}

I want to replace only already existing items from second object to first one. Result should look like this (so only b item should be changed, and new d item ignored):

{
   a: 'John',
   b: 55, // changed
   c: 'example'
}

Merge will not work because it will add also new item. I can use foreach but I believe that there should be shorted answer for this. I'm already using lodash in my project so I can use function from there, but I cannot find any for this purpose. Is there any?

6 Answers

You could use this if you use ES6

let merge = { ...first, ..._.pick(second, Object.keys(first)) }

With lodash you could do something like this with _.merge, _.pick and _.keys:

let first = {
   a: 'John',
   b: 22,
   c: 'example'
}, second = {
   b: 55,
   d: 'demo'
}

let result = _.merge(first, _.pick(second, _.keys(first)))

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

With ES6 you can use Object.keys and then Array.forEach on a new object like this:

let first = {
   a: 'John',
   b: 22,
   c: 'example'
}, second = {
   b: 55,
   d: 'demo'
}, result = new Object(null)

Object.keys(first).forEach(k => result[k] = second[k] || first[k])

console.log(result)

This assumes you do not want to mutate any of the objects. IF you do not care:

let first = {
   a: 'John',
   b: 22,
   c: 'example'
}, second = {
   b: 55,
   d: 'demo'
}

Object.keys(first).forEach(k => first[k] = second[k] || first[k])

console.log(first)

You can use a loop over the keys in the second array. For any keys that exist in the first, overwrite the value.

let first = {
   a: 'John',
   b: 22,
   c: 'example'
}

let second = {
   b: 55,
   d: 'demo'
}

for (const k in second) {
  if (k in first) {
    first[k] = second[k];
  }
}

console.log(first);

You want to update the values of the

intersection

of the properties.

So basically something like:

Object.keys(a).forEach(prop => if(b.hasOwnProperty(prop)) a[prop] = b[prop]))

Or

_.intersection(Object.keys(a), Object.keys(b)).forEach(prop => a[prop] = b[prop])

let first = {
   a: 'John',
   b: 22,
   c: 'example'
}

let second = {
   b: 55,
   d: 'demo'
}

Object.keys(second).forEach(function(key,index){
  if (first.hasOwnProperty(key)) {
        first[key]=second[key];
  }
});

console.log(first);

With Lodash two functions MergeWith and Pick

var a = {
   a: 'John',
   b: 22,
   f:[11,22,2],
   d: {a:1,b:2,c:0},
   c: 'example'
}

var b = {
   b: 55,
   f:[3],
   d: {c:1,b:11},
}

function mergeObjects(a,b){
  let common = _.pick(b, Object.keys(a));
  return _.mergeWith(a, common, customizer)
}

function customizer(objValue, srcValue) {
  if (_.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
  if(_.isObject(objValue)) {
      return mergeObjects(objValue,srcValue)
  }
}
mergeObjects(a,b)

Related