Javascript 替换VueJs数组中的值
我有两个阵列: 1)Javascript 替换VueJs数组中的值,javascript,arrays,vue.js,Javascript,Arrays,Vue.js,我有两个阵列: 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"} 0: {name: "Number One", position: "373", additionalinfo:
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"}
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"}
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"}
2) displayArray
格式如下:
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"}
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"}
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"}
我希望第二个数组displayArray
使用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"}
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"}
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"}
我不介意创建第三个数组,但当我试图在数组中使用它时,我遇到了错误。我该怎么做?选项1:更改displayArray
使用forEach的:
displayArray.forEach((项目,索引)=>{
item.position=listArray[index].text;
})
选项2:创建新阵列
使用映射
:
const newArray=displayArray.map((项目,索引)=>{
const newitem=Object.assign({},item);
newitem.position=listArray[index].text;
返回新项目;
})
下面是一个您尝试了什么,出现了哪些错误?一个forEach
或map
应该可以做到这一点。从数组1获取数据并替换数组2会是一个什么样的条件呢?这不是一个错误,而是用数组1的文本值替换数组2的位置值不起作用