我有一个具有以下值的javascript数组,
let arr = ["2","3","4","5","6"];
我有第二个有三个整数的数组,
let index = [1,3,4];
我如何使用JQuery(或Javascript)来使用数组从arr获取索引,并将其添加到新数组中。我想将这些字符串添加到新数组中。
indexArr = ["3","5","6"]
由于
答案 0 :(得分:1)
简单,只需使用Array.prototype.map()
:
let arr = ["2","3","4","5","6"];
let index = [1,3,4];
let indexArr = index.map(i => arr[i]);
console.log(indexArr);

答案 1 :(得分:1)
你可以用简单的.map
来做let arr = ["2","3","4","5","6"];
let index = [1,3,4];
index.map(x=>arr[x]) //["3", "5", "6"]
答案 2 :(得分:0)
只需使用循环。我个人更喜欢这种方法而不是map
因为我觉得这更容易理解,但这是个人偏好。
let arr = ["2","3","4","5","6"];
let index = [1,3,4];
let indexArr = [];
for(var i=0; i<index.length; i++) {
indexArr.push(arr[index[i]]);
}
console.log(indexArr);
&#13;