find(), filter(), map() and reduce()
This article contains about commonly used array methods in Javascript.
The find(), filter(), map(), and reduce() methods are powerful tools for working with arrays in JavaScript. They provide concise ways to find specific elements, filter elements based on conditions, transform array elements into new values, and perform calculations on arrays.
find():
const array = [1, 2, 3, 4, 5];
const foundElement = array.find(element => element > 2);
console.log(foundElement); // Output: 3
filter():
const array = [1, 2, 3, 4, 5];
const filteredArray = array.filter(element => element % 2 === 0);
console.log(filteredArray); // Output: [2, 4]
map():
const array = [1, 2, 3, 4, 5];
const mappedArray = array.map(element => element * 2);
console.log(mappedArray); // Output: [2, 4, 6, 8, 10]
reduce():
const array = [1, 2, 3, 4, 5];
const sum = array.reduce((accumulator, element) => accumulator + element, 0);
console.log(sum); // Output: 15
These methods are commonly used in JavaScript to perform various operations on arrays.
Love this 😉
Great job bro 👏 keep rocking 🎉