简体   繁体   English

Javascript:如何在某些索引位置获取数组的值

[英]Javascript: How to get values of an array at certain index positions

I have an array of values我有一个值数组

arr = ["a","b","c","d"]

and I have another array of indexes我还有另一个索引数组

indexes = [0,2]

What is the best way to get the values of the array at these indexes ?在这些索引处获取数组值的最佳方法是什么?

If I apply the method to the values above如果我将该方法应用于上述值

it should return它应该返回

["a","c"]

Use Array.map :使用Array.map

 arr = ["a","b","c","d"] indexes = [0,2] const res = indexes.map(e => arr[e]) console.log(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM