How to filter data from one array based on another array February 16, 2021 by admin Suppose i have 2 array arr1=["a","b","c","d","e"] arr2 = [1,3] Required output arr1 = ["b","d"]
You could use Array.filter and Array.includes: const arr1 = ['a', 'b', 'c', 'd', 'e']; const arr2 = [1, 3] const arr3 = arr1.filter((_, i) => arr2.includes(i)); console.log(arr3); Reply
I assume that you want the value of the first array with the index provided in the second array. You can achieve that with the following approach arr2.forEach(value => d.push(arr1[value])) Reply
You could use
Array.filter
andArray.includes
:I assume that you want the value of the first array with the index provided in the second array. You can achieve that with the following approach