简体   繁体   English

嵌套对象分配 Javascript

[英]Nested object assign Javascript

I have two objects like this:我有两个这样的对象:

var a = {prop:{a:'1'}}
var b = {prop:{b:'1'}}

I want to merge the two objects using Object.assign.我想使用 Object.assign 合并两个对象。 I tried this:我试过这个:

var c = Object.assign(a,b)
> {prop:{b:'1'}}

How do I get this output:我如何获得此输出:

{prop:{a:'1',b:'1'}}

In short, I want to do Object assign on every sub Objects => (IF PROPERTY EXISTS, CHECK CHILDREN FIRST IF IT AS OBJECT, DO NOT OVERIDE)简而言之,我想对每个子对象进行对象分配 =>(如果存在财产,请首先检查儿童,如果它是对象,请勿覆盖)

You could use a deep merge function for every level of the object. 您可以为对象的每个级别使用深度合并功能。

The function deepMerge works as single function for a given target or as callback for Array#reduce , where an array of objects is iterated and an empty object is supplied as startvalue for reducing. 函数deepMerge作为给定目标的单个函数或作为Array#reduce回调,其中迭代一个对象数组,并提供一个空对象作为还原的startvalue

The function/callback itselft has two parameters, one for the target object and one for the source object where all entries are taken and iterated. 函数/回调itselft有两个参数,一个用于目标对象,另一个用于源对象,其中所有条目都被采用和迭代。

A value of the object is checked and if the type is an object, then a recursive call of deepMerge is done with aa new target property, if not given or to an exitent property. 检查对象的值,如果类型是对象,则使用新的目标属性(如果未给出)或exitent属性对deepMerge进行递归调用。

If the value is not an object, the value is assigned to target with the given key. 如果该值不是对象,则使用给定键将值分配给target

The result is a new object with all properties of the given objects. 结果是一个具有给定对象的所有属性的新对象。

 function deepMerge(target, source) { Object.entries(source).forEach(([key, value]) => { if (value && typeof value === 'object') { deepMerge(target[key] = target[key] || {}, value); return; } target[key] = value; }); return target; } var a = { prop: { a: '1' } }, b = { prop: { b: '1' } }, c = [a, b].reduce(deepMerge, {}); console.log(c); 

Since a and b in your case contains a property with same name, when you do something like Object.assign(a,b) the prop property of b overwrites and not merge the prop. 由于你的案例中的a和b包含一个具有相同名称的属性,当你执行像Object.assign(a,b)这样的事情时Object.assign(a,b)prop属性会覆盖并且不会合并prop。 Reference 参考

Try the following: 请尝试以下方法:

 var a = {prop:{a:'1'}} var b = {prop:{b:'1'}} var c = {}; c.prop = Object.assign({},a.prop,b.prop); console.log(c); 

Or 要么

You can write your own custom function which can merge the values of the objects with same properties. 您可以编写自己的自定义函数,该函数可以合并具有相同属性的对象的值。

Try the following: 请尝试以下方法:

 function merge(source, target) { Object.keys(source).forEach((k) => { if (source[k].constructor.toString().indexOf('Object') > -1 && target[k]) { merge(source[k], target[k]); return; } target[k] = source[k]; }); return target; } var a = {prop:{a: '1'}}; var b = {prop:{ b: '1'}}; var c = {}; [a,b].forEach((e)=>merge(e, c)); console.log(c); 

If Object.assign is no hard requirement, you could do this in es6: 如果Object.assign没有硬性要求,你可以在es6中这样做:

 var c = {prop:{...a.prop, ...b.prop}}

See MDN on Spread syntax 请参阅扩展语法上的 MDN

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

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