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

后端 未结 5 2133
没有蜡笔的小新
没有蜡笔的小新 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:06

    there's an easy way to change object property without mutating the original object.

    const a = {x: "Hi", y: "Test"};
    const b = {...a, y: "hello"}; 

提交回复
热议问题