简体   繁体   English

纯JS或Lodash-用其他对象的值替换对象值

[英]Plain JS or Lodash - replace object values with values from other object

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): 结果应如下所示(因此仅应更改b项,而忽略新的d项):

{
   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. 我可以使用foreach,但我认为应该对此做一个简短的回答。 I'm already using lodash in my project so I can use function from there, but I cannot find any for this purpose. 我已经在我的项目中使用lodash,因此可以从那里使用功能,但是我找不到用于此目的的任何功能。 Is there any? 有没有?

如果使用ES6,则可以使用它

let merge = { ...first, ..._.pick(second, Object.keys(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); 

With lodash you could do something like this with _.merge , _.pick and _.keys : 使用lodash,您可以使用_.merge_.pick_.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: 使用ES6,您可以在新对象上使用Object.keysArray.forEach ,如下所示:

 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. 假设您不想mutate任何对象。 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 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 使用Lodash的两个功能MergeWithPick

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM