Map and Filter are two awsome Javascript Array Methods used for array manipulation. In this bog “Map vs. Filter in JavaScript” we will try to understand their distinct purposes and applications.
Let’s delve into their differences:
Contents
Map: Making a New Array with Transformed Elements
Purpose:
Map is one of the most used Javascript Array Methods that creates a new array by applying a given function to each element of the original array, allowing you to modify values or produce entirely new data.
Syntax:
Array.prototype.map(callbackFn, thisArg)
callbackFn
: Function executed for each element, taking three arguments:currentValue
: Current element being processed.index
: Index of the current element.array
: The original array being iterated over.
thisArg
: Optional. Value to be used asthis
incallbackFn
.
Working:
- Returns a new array with the same length as the original.
- Elements in the new array are the results of applying
callbackFn
to each corresponding element in the original array. callbackFn
can return any value, and those values form the elements of the new array.
Examples:
const numbers = [1, 2, 3, 4];
const doubledNumbers = numbers.map(x => x * 2); // [2, 4, 6, 8]
const users = [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }];
const usernames = users.map(user => user.name); // ['Alice', 'Bob']
const fruits = ['apple', 'banana', 'orange'];
const capitalizedFruits = fruits.map(fruit => fruit.toUpperCase()); // ['APPLE', 'BANANA', 'ORANGE']
Filter: Selecting Elements Based on a Condition
Purpose:
Creates a new array containing only the elements from the original array that pass a test implemented by a provided function.
Syntax:
Array.prototype.filter(callbackFn, thisArg)
callbackFn
: Function executed for each element, taking the same arguments asmap
.thisArg
: Optional. Value to be used asthis
incallbackFn
.
Working:
- Returns a new array that may be shorter or equal in length to the original array.
- Elements in the new array are those for which
callbackFn
returnedtrue
. callbackFn
typically returns a boolean value to indicate whether to include the element.
Examples:
const evenNumbers = numbers.filter(x => x % 2 === 0); // [2, 4]
const adults = users.filter(user => user.age >= 21); // [{ name: 'Bob', age: 30 }]
const citrusFruits = fruits.filter(fruit => ['orange', 'lemon', 'lime'].includes(fruit)); // ['orange']
Key Differences and When to Use Each:
- Transformation vs. Selection:
map
transforms elements, whilefilter
selects them based on criteria. - New Array Length:
map
always creates a new array with the same length, whilefilter
‘s new array can be shorter or equal. - Element Values:
map
produces an array with modified or entirely new values, whilefilter
retains the original values of selected elements.
When to Use map
:
- Modify element values according to a rule (e.g., doubling numbers, capitalizing strings).
- Create a new array of derived data (e.g., usernames from an array of users).
- Implement more complex transformations using chained methods (e.g.,
map().filter()
).
When to Use filter
:
- Remove elements that don’t meet a certain condition (e.g., filtering even numbers).
- Create a subset of elements based on user input or dynamic criteria.
- Combine with other methods for focused data manipulation (e.g.,
filter().map()
).
To Conclude:
By reading “Map vs. Filter In Javascript Array Methods” you have elevated your array manipulation skills in JavaScript. Remember their fundamental purposes, and experiment with them to discover their potential. Thanks for reading!
Also Read: https://deepakjosecodes.com/slice-vs-splice-in-javascript-a-comprehensive-comparison/
Comment down your thoughts and suggestions for future posts