5

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 6

4

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)

Sign up to request clarification or add additional context in comments.

Comments

3

You could use this if you use ES6

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

Comments

2

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

Comments

0

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])

Comments

0

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.