如何根据另一个数组过滤一个数组中的数据

时间:2021-02-16 05:48:45

标签: javascript ecmascript-6

假设我有 2 个数组 arr1=["a","b","c","d","e"] 基于arr2 = [1,3] 所需的输出为 arr1 = ["b","d"]

4 个答案:

答案 0 :(得分:1)

valueArray = ["a","b","c","d","e"]; // arr1
indexArray = [1,3]; // arr2

valueArray = indexArray.map(index => valueArray[index]);

console.log(valueArray);

答案 1 :(得分:1)

只需执行以下顺序:

const arr1=["a","b","c","d","e"];
const arr2 = [1,3];
const filteredArray = arr1.filter((_,index)=>{
 return arr2.includes(index)
});
console.log(filteredArray)

答案 2 :(得分:0)

我假设您想要第一个数组的值以及第二个数组中提供的索引。您可以通过以下方法实现这一点

arr2.forEach(value => d.push(arr1[value]))

答案 3 :(得分:0)

您可以使用地图:

[PC]\(\d+[ ,]\s*\d+\)