如何根据索引数组获取值数组? [英] How to get an array of values based on an array of indexes?
本文介绍了如何根据索引数组获取值数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组两个数组.一个包含一些作为字符串的水果值,另一个包含一些随机数.在这里,我认为数字数组是水果数组的索引.给定索引数组中的数字,如何获取新的水果数组?
I have a set of two arrays. One contains some fruit values as strings, the other one contains some random numbers. Here I considered the number arrays are the indexes of the fruits array. How to get a new array of fruits given the numbers in the index array?
示例代码:
var resultArr = [];
var fruitier = ["apple", "orange", "grapes", "pineapple", "fig", "banana", "jackfruit", "pomegranate"];
var indexArr = [0, 2, 4];
输出:
resultArr = ["apple", "grapes", "fig"];
推荐答案
for(var i = 0; i < indexArr.length; i++)
resultArr.push(fruitier[indexArr[i]]);
这篇关于如何根据索引数组获取值数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文