如何根据索引数组获取值数组?

时间:2022-07-22 21:16:45

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?

我有一组两个数组。一个包含一些水果值作为字符串,另一个包含一些随机数。这里我认为数字数组是fruits数组的索引。如何根据索引数组中的数字获得新的水果数组?

Sample code:

var resultArr = [];
var fruitier = ["apple", "orange", "grapes", "pineapple", "fig", "banana", "jackfruit", "pomegranate"];
var indexArr = [0, 2, 4];

Output:

resultArr = ["apple", "grapes", "fig"];

5 个解决方案

#1


4  

for(var i = 0; i < indexArr.length; i++)
  resultArr.push(fruitier[indexArr[i]]);

#2


9  

Use .map:

var resultArr = indexArr.map(i => fruitier[i])

#3


6  

If you want to achieve that with lodash, use _.at():

如果你想用lodash实现,使用_.at():

var fruitier = ['apple', 'orange', 'grapes', 'pineapple', 'fig', 'banana', 'jackfruit', 'pomegranate'];
var indexArr = [0, 2, 4];
var resultArr = _.at(fruitier, indexArr);

console.log(resultArr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>

#4


1  

You can use for..of loop

您可以使用for..of循环

for (var key of indexArr) resultArr[resultArr.length] = fruitier[key];

#5


1  

Array#map works (documentation)

数组#map工作(文档)

const getFromIndex = (array, indexes) => indexes.map((index) => array[index]);

You can use Array#filter too (documentation)

您也可以使用Array#filter(文档)

const fruitier = ['apple', 'orange', 'grapes', 'pineapple', 'fig',   'banana', 'jackfruit', 'pomegranate'];
const indexArr = [0, 2, 4];  

const getFromIndex = (array, indexes) => {
  return array.filter((element, index) => indexes.includes(index)); 
};

Or also Array#reduce (documentation)

或者还有Array#reduce(文档)

const getFromIndex = (array, indexes) => {
    return indexes.reduce((result, i) => result.concat(array[i]), []); 
};

#1


4  

for(var i = 0; i < indexArr.length; i++)
  resultArr.push(fruitier[indexArr[i]]);

#2


9  

Use .map:

var resultArr = indexArr.map(i => fruitier[i])

#3


6  

If you want to achieve that with lodash, use _.at():

如果你想用lodash实现,使用_.at():

var fruitier = ['apple', 'orange', 'grapes', 'pineapple', 'fig', 'banana', 'jackfruit', 'pomegranate'];
var indexArr = [0, 2, 4];
var resultArr = _.at(fruitier, indexArr);

console.log(resultArr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>

#4


1  

You can use for..of loop

您可以使用for..of循环

for (var key of indexArr) resultArr[resultArr.length] = fruitier[key];

#5


1  

Array#map works (documentation)

数组#map工作(文档)

const getFromIndex = (array, indexes) => indexes.map((index) => array[index]);

You can use Array#filter too (documentation)

您也可以使用Array#filter(文档)

const fruitier = ['apple', 'orange', 'grapes', 'pineapple', 'fig',   'banana', 'jackfruit', 'pomegranate'];
const indexArr = [0, 2, 4];  

const getFromIndex = (array, indexes) => {
  return array.filter((element, index) => indexes.includes(index)); 
};

Or also Array#reduce (documentation)

或者还有Array#reduce(文档)

const getFromIndex = (array, indexes) => {
    return indexes.reduce((result, i) => result.concat(array[i]), []); 
};