I'm currently working on some homework and encounter some weird behavior ( to me ) about changing properties value in a object . Show in below code :
// let say I have a object
const a = {
name:"origin",
arr:["origin"],
}
// then I create another object and fill that object with props of "a"
const b = {...a}
// then doing some mutate with props of "b"
b.name = "mutated";
b.arr.push("mutated");
// but it also change "arr" props of "a" (name is not change)
console.log(a.name); // log "origin"
console.log(a.arr);// log ["origin", "mutated"]
I don't understand why it also changed the props of a
. Can anyone explain it for me ? Am I using the wrong technology here (...
) ? Thanks a lot