Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Both Object.assign and Object spread only do a shallow merge.

(Object.assignObject Spread都仅进行浅合并。)

An example of the problem:

(问题的一个示例:)

// No object nesting
const x = { a: 1 }
const y = { b: 1 }
const z = { ...x, ...y } // { a: 1, b: 1 }

The output is what you'd expect.

(输出是您期望的。)

However if I try this:

(但是,如果我尝试这样做:)

// Object nesting
const x = { a: { a: 1 } }
const y = { a: { b: 1 } }
const z = { ...x, ...y } // { a: { b: 1 } }

Instead of

(代替)

{ a: { a: 1, b: 1 } }

you get

(你得到)

{ a: { b: 1 } }

x is completely overwritten because the spread syntax only goes one level deep.

(x完全被覆盖,因为传播语法仅深入了一层。)

This is the same with Object.assign() .

(这与Object.assign()相同。)

Is there a way to do this?

(有没有办法做到这一点?)

  ask by Mike translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
381 views
Welcome To Ask or Share your Answers For Others

1 Answer

Does anybody know if deep merging exists in the ES6/ES7 spec?

(有人知道ES6 / ES7规范中是否存在深度合并?)

No, it does not.

(不,不是的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...