cosnt config = {a: {x: 1, y: 2, z: 3}, b: {x: 12, y: 23, z: 34}};
console.log(config);
// ->
// [object Object] {
// a: [object Object] {
// x: 1,
// y: 2,
// z: 3
// },
// b: [object Object] {
// x: 12,
// y: 23,
// z: 34
// }
// }
const items = [];
items.push(config['a']);
console.log(items);
// ->
// [[object Object] {
// x: 1,
// y: 2,
// z: 3
// }]
items[items.length - 1].q = "mutation";
console.log(items);
// ->
// [[object Object] {
// q: "mutation",
// x: 1,
// y: 2,
// z: 3
// }]
console.log(config);
// ->
// [object Object] {
// a: [object Object] {
// q: "mutation",
// x: 1,
// y: 2,
// z: 3
// },
// b: [object Object] {
// x: 12,
// y: 23,
// z: 34
// }
// }
console.log(config.a)
// ->
// [object Object] {
// q: "mutation",
// x: 1,
// y: 2,
// z: 3
// }
I understand that config.a
is updating because items[0]
is a reference to config.a
Still it doesn't make much sense to me why its designed this way.
I'm thinking about how to code this better so that it doesn't update config