In JavaScript, use the filter() method of an array and another array as a filter condition to filter an array.

array filter array

In JavaScript, you can filter an array using the filter() method of the array and another array as the filter condition.

Here's a sample code to filter the original array using another array as a filter condition:

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const filterArr = [2, 4, 6, 8, 10];

const filteredArr = arr.filter(num => filterArr.includes(num));

console.log(filteredArr); // [2, 4, 6, 8, 10]

In this example, the filter() method takes an anonymous function as a parameter, which checks whether each element in the array appears in the filterArr array. If an element in the array occurs in filterArr, that element is included in the new array returned.

Using this method, you can dynamically filter elements from one array through another array.

Guess you like

Origin blog.csdn.net/NIKKT/article/details/130427272