gpt4 book ai didi

javascript - 克隆数组(不是引用副本)并修改新数组

转载 作者:行者123 更新时间:2023-12-04 09:30:47 25 4
gpt4 key购买 nike

我认为我的问题很容易解决,但作为新手我无法到达那里。

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)
日志显示,forEach() 函数也应用于 arr1。为什么会这样,我如何才能保持 arr1 不可变?
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包含对象,所以只需克隆 arr1是不足够的。您需要克隆 arr1 中的对象也。
您可以使用 .map()函数和扩展运算符创建一个新数组,该数组包含 arr1 中对象的克隆。 .

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.map(obj => ({...obj}));

testArr.forEach(obj => (obj.status = "test"));

console.log(arr1[0]);
console.log(testArr[0]);
.as-console-wrapper { max-height: 100% !important; top: 0; } 

使用 .map()函数还允许您更新返回的对象。所以你可以删除 forEach循环并使用 .map()函数不仅可以克隆对象,还可以更新 status克隆对象的属性。

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.map(obj => ({...obj, status: 'test'}));

console.log(arr1[0]);
console.log(testArr[0]);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 克隆数组(不是引用副本)并修改新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62861485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com