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

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

    Try this : const b = a.map(item => Object.assign({}, ...item)); This will create a new object without any reference to the old object a

    In case if you want to do this for an array try the same with []

    const b = a.map(item => Object.assign([], ...item));
    

提交回复
热议问题