forEach()
Executes a provided function once for each array element.
Example:
let fruits = ['Apple', 'Banana', 'Mango'];
fruits.forEach(function(item, index) {
console.log(index, item);
});
// Output: 0 Apple, 1 Banana, 2 Mango
map()
Creates a new array populated with the results of calling a provided function on every element.
Example:
let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(function(number) {
return number * 2;
});
console.log(doubled); // Output: [2, 4, 6, 8, 10]
filter()
Creates a new array with all elements that pass the test implemented by the provided function.
Example:
let numbers = [1, 2, 3, 4, 5];
let even = numbers.filter(function(number) {
return number % 2 === 0;
});
console.log(even); // Output: [2, 4]
reduce()
Executes a reducer function on each element of the array, resulting in a single output value.
Example:
let numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce(function(total, number) {
return total + number;
}, 0);
console.log(sum); // Output: 15
flatMap()
Maps each element using a mapping function, then flattens the result into a new array.
Example:
let numbers = [1, 2, 3, 4];
let mapped = numbers.flatMap(x => [x * 2]);
console.log(mapped); // Output: [2, 4, 6, 8]