How to change a property on an object without mutating it?

后端 未结 5 2134
没有蜡笔的小新
没有蜡笔的小新 2021-02-19 00:40
const a = {x: "Hi", y: "Test"}
const b = ???
// b = {x: "Bye", y: "Test"}
// a = {x: "Hi", y: "Test"}
         


        
5条回答
  •  遥遥无期
    2021-02-19 01:13

    Create a copy of the object and change the copy:

    const b = Object.assign({}, a, {x: "Bye"})
    

    Docs for Object.assign()

提交回复
热议问题