PM
Size: a a a
PM
AP
М
PM
М
PM
PM
М
PM
А
let array = [1, 2, 3];
let temp = array;
temp[0] = 5; // изменяет и оригинал
console.log(1, JSON.stringify(array));
console.log(2, JSON.stringify(temp));
let array = [{ name: "val1" }, { name: "val2" }];
let temp = array;
temp[0] = 5; // изменяет и оригинал
console.log(1, JSON.stringify(array));
console.log(2, JSON.stringify(temp));
let array = [{ name: "val1" }, { name: "val2" }];
let temp = array;
temp[0].name = "qwe"; // изменяет и оригинал
console.log(1, JSON.stringify(array));
console.log(2, JSON.stringify(temp));
let array = [1, 2, 3];
let temp = [...array];
temp[0] = 5; // изменяет только копию
console.log(1, JSON.stringify(array));
console.log(2, JSON.stringify(temp));
let array = [{ name: "val1" }, { name: "val2" }];
let temp = [...array];
temp[0] = 5; // изменяет только копию
console.log(1, JSON.stringify(array));
console.log(2, JSON.stringify(temp));
let array = [{ name: "val1" }, { name: "val2" }];
let temp = JSON.parse(JSON.stringify(array));
temp[0].name = "qwe"; // изменяет только копию
console.log(1, JSON.stringify(array));
console.log(2, JSON.stringify(temp));
let array = [{ name: "val1" }, { name: "val2" }];
let temp = [...array]; // или Array.from(array), array.map(x => x), array.slice()
temp[0] = 5; // изменяет только копию
console.log(1, JSON.stringify(array));
console.log(2, JSON.stringify(temp));
temp[1].name = "qwe"; // изменяет и оригинал!
console.log(1, JSON.stringify(array));
console.log(2, JSON.stringify(temp));
AP
import produce from 'immer';https://immerjs.github.io/immer/
const temp = produce(array, (draft) => { draft[0] = 5 })
EM
temp = arr.map(el => ({...el}))
А
К
К
AP
KK