如果我有一个字符串数组:
array1 = ['mango', 'orange', 'apple','strawberry'];
和一个索引数组:
array2 = [1,3];
如何基于array2中的索引使用array1中的字符串创建一个新数组?
在这种情况下,新数组如下所示:
array3 = [orange, strawberry]
答案 0 :(得分:0)
//Declare a new array object
let newArray = [];
//iterate the array of index values, and use the value to index the first array and push into new array
for(let i = 0; i < array2.length; i++){
newArray.push(array1[array2[i]]);
}
//log the results in new array
console.log(newArray);