Javascript: 如何获取数组中特定索引位置的值
Javascript: How to get values of an array at certain index positions
我有一组值
arr = ["a","b","c","d"]
我还有另一个索引数组
indexes = [0,2]
在这些索引处获取数组值的最佳方法是什么?
如果我将方法应用于上述值
应该return
["a","c"]
使用Array.map
:
arr = ["a","b","c","d"]
indexes = [0,2]
const res = indexes.map(e => arr[e])
console.log(res)
我有一组值
arr = ["a","b","c","d"]
我还有另一个索引数组
indexes = [0,2]
在这些索引处获取数组值的最佳方法是什么?
如果我将方法应用于上述值
应该return
["a","c"]
使用Array.map
:
arr = ["a","b","c","d"]
indexes = [0,2]
const res = indexes.map(e => arr[e])
console.log(res)