gpt4 book ai didi

javascript - 替换 VueJs 数组中的值

转载 作者:行者123 更新时间:2023-12-05 06:19:47 25 4
gpt4 key购买 nike

我有两个数组:

1) listArray 格式如下:

0: {value: "373", text: "1 - First Item in the Array"}
1: {value: "343", text: "2 - Second Item in the Array"}
2: {value: "376", text: "3 - Third Item in the Array"}

2) displayArray 格式如下:

0: {name: "Number One", position: "373", additionalinfo: "Description Goes Here"}
1: {name: "Number Two", position: "343", additionalinfo: "Description Goes Here"}
2: {name: "Number three", position: "376", additionalinfo: "Description Goes Here"}

我想让第二个数组 displayArray 使用 listArray 中的信息,看起来像这样:

0: {name: "Number One", position: "1 - First Item in the Array", additionalinfo: "Description Goes Here"}
1: {name: "Number Two", position: "2 - Second Item in the Array", additionalinfo: "Description Goes Here"}
2: {name: "Number three", position: "3 - Third Item in the Array", additionalinfo: "Description Goes Here"}

我不介意创建第三个数组,但是当我尝试在 array.push 中使用它时,出现错误。我该怎么做?

最佳答案

选项 1:更改 displayArray

使用forEach:

displayArray.forEach((item, index) => {
item.position = listArray[index].text;
})

选项 2:创建一个新数组

使用 map :

const newArray = displayArray.map((item, index) => {
const newitem = Object.assign({}, item);
newitem.position = listArray[index].text;
return newitem;
})

这是一个demo

关于javascript - 替换 VueJs 数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60687830/

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