Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To filter an array using the values in another array, you can use the filter() method and pass a function that checks if the value of the first array is included in the second array.

For example:

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [2, 4, 6];

const filteredArray = arr1.filter(value => arr2.includes(value));

console.log(filteredArray); // [2, 4]

In this example, the filter() method checks each value in arr1 and includes() method checks if it's present in arr2. If it is, the value is added to the filteredArray.

Note: This method assumes that both arrays contain only primitive types (numbers, strings, booleans) and does not work with complex objects.