I assume my issue is easy to fix but as beeing a newbee I can not get there.
const arr1 = [
{ department: "Lebensmittel", id: "id6", product: "Nudeln", status: false },
{ department: "Ceralien", id: "id5", product: "Marmelade", status: false },
{ department: "Ceralien", id: "id3", product: "Müsli", status: false },
{ department: "Ceralien", id: "id4", product: "Honig", status: false },
{ department: "Molkereiprodukte", id: "id1", product: "Milch", status: false }
];
let testArr = [...arr1];
testArr.forEach(obj => (obj.status = "test"));
console.log(arr1, testArr)
The log shows, that the forEach() functions gets also applied on the arr1. Why is this and how can I keep arr1 unmutable?
arr1 = [
{ department: "Lebensmittel", id: "id6", product: "Nudeln", status: false },
{ department: "Ceralien", id: "id5", product: "Marmelade", status: false },
{ department: "Ceralien", id: "id3", product: "Müsli", status: false },
{ department: "Ceralien", id: "id4", product: "Honig", status: false },
{ department: "Molkereiprodukte", id: "id1", product: "Milch", status: false }
];
testArr = [
{ department: "Lebensmittel", id: "id6", product: "Nudeln", status: false },
{ department: "Ceralien", id: "id5", product: "Marmelade", status: false },
{ department: "Ceralien", id: "id3", product: "Müsli", status: false },
{ department: "Ceralien", id: "id4", product: "Honig", status: false },
{ department: "Molkereiprodukte", id: "id1", product: "Milch", status: false }
];

[...arr1]only makes a shallow copy, the objects inside are not copied so references are the same between the two arrays