Need to filter an array in JavaScript? Follow these easy steps:
1. Create an Array:
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
2. Apply the Filter Method:
filter() method to create a new array with elements that match the criteria:
const evenNumbers = numbers.filter(number => number % 2 === 0);
3. Check the Result:
console.log(evenNumbers); // [2, 4, 6, 8, 10]
4. Filter with Custom Criteria:
const words = ['apple', 'banana', 'cherry', 'date'];
const longWords = words.filter(word => word.length > 5);
console.log(longWords); // ['banana', 'cherry']
Jorge García
Fullstack developer