Array Filtering, Mapping and Reducing in JavaScript | Javascript

 // Filtering array
const filteredArray = myArray.filter(item => /* condition */);

// Mapping array
const mappedArray = myArray.map(item => /* transformation */);

// Reducing array
const reducedValue = myArray.reduce((accumulator, currentValue) => /* aggregation */, initialValue);
 

AI Explanation

This code snippet demonstrates common array manipulation methods in JavaScript: `.filter()`, `.map()`, and `.reduce()`. 1. Filtering Array: The `.filter()` method creates a new array with all elements that pass the test implemented by the provided function. In the `filteredArray` declaration, `myArray` is being filtered based on the provided condition or test specified inside the arrow function. Only elements that meet the condition will be included in the `filteredArray`. ```javascript const filteredArray = myArray.filter(item => /* condition */); ``` 2. Mapping Array: The `.map()` method iterates over each element in an array and transforms it based on the provided function. The `mappedArray` declaration creates a new array by applying the specified transformation to each element in `myArray`. ```javascript const mappedArray = myArray.map(item => /* transformation */); ``` 3. Reducing Array: The `.reduce()` method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. In the `reduce()` usage, the provided function aggregates the elements of `myArray` into a single value (`reducedValue`) by performing the specified aggregation operation. An `initialValue` can also be provided as a starting value for the accumulation process. ```javascript const reducedValue = myArray.reduce((accumulator, currentValue) => /* aggregation */, initialValue); ``` These array methods are powerful tools in JavaScript for manipulating arrays in various ways, filtering elements, transforming elements, and aggregating values.