How to Filter an Array of Arrays in JavaScript?

Estimated read time 1 min read

To filter an array of arrays in JavaScript, you can use the filter method of the Array object. The filter method creates a new array with all elements that pass the test implemented by the provided function. Here’s an example:

const arrOfArrs = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const filteredArr = arrOfArrs.filter(subArr => subArr.length > 2);

// Output: [[1, 2, 3], [4, 5, 6]]

In this example, the filter method is used to filter the arrOfArrs array and return only those sub-arrays whose length is greater than 2. The resulting filtered array is logged to the console.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply