Skip to content

Commit 587fc55

Browse files
feat: add map vs filter
1 parent a5f0f4c commit 587fc55

File tree

3 files changed

+69
-44
lines changed

3 files changed

+69
-44
lines changed

11_array/array_method/Map.js

-44
This file was deleted.

19_Iterations/9_map.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// `map()`
2+
3+
// What
4+
// - .map() is a built-in method for arrays in JavaScript. It creates a new array by applying a function to all elements of the original array.
5+
// - It transforms each element in the original array based on the provided function, giving you a new array with the results.
6+
7+
// Why
8+
// - It offers a concise and efficient way to perform the same operation on every element in an array, creating a new transformed array.
9+
// - It avoids repetitive code for iterating through an array and modifying each element individually.
10+
11+
// How
12+
// - You provide a callback function to .map(). This function is called for each element in the original array.
13+
// - The callback function takes three arguments (though the last two are optional):
14+
// - currentValue: The current element being processed in the iteration.
15+
// - index (optional): The index of the current element (if needed).
16+
// - array (optional): The original array being iterated over (if needed).
17+
// - The callback function should return a value that will be included in the new array.
18+
19+
// Syntax
20+
// const newArray = originalArray.map(callbackFunction);
21+
22+
// Example
23+
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
24+
const newNumbers = array.map((number) => number < 10);
25+
console.log(newNumbers);
26+
27+
// EXAMPLE
28+
const array1 = [
29+
{
30+
key1: "value1",
31+
key2: "value2",
32+
key3: "value3",
33+
sameKey: "same value",
34+
},
35+
{
36+
key4: "value4",
37+
key5: "value5",
38+
key6: "value6",
39+
sameKey: "same value",
40+
},
41+
];
42+
43+
const newArray3 = array1.map((object) => {
44+
return object.sameKey;
45+
});
46+
console.log(newArray3);

x_notes/terms_explain.md

+23
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ Cons
3333
| - It store data in linear form. | - It store data in `key : value` form. |
3434
| | - It has size property. |
3535

36+
# .map() VS .filter()
37+
38+
| Feature | `filter` | `map` |
39+
| ---------------------------- | --------------------------------------------------------------- | --------------------------------------------------------------------------- |
40+
| **Purpose** | Filter elements based on a condition | Transform elements and create a new array |
41+
| **Callback function output** | `true` or `false` (includes/excludes) | A value to be included in the new array |
42+
| **New array elements** | Only elements for which `true` is returned | Transformed values from the callback function |
43+
| **Modifies original array** | No | No (creates a new array) |
44+
| **Side effects** | Can be used for actions within the loop (e.g., logging) | May have side effects within the loop (e.g., modifying temporary variables) |
45+
| **Functional approach** | More functional, focuses on filtering logic | More functional, focuses on transformation logic |
46+
| **Common use cases** | - Finding specific elements | - Doubling numbers |
47+
| | - Removing elements from an array | - Converting strings to uppercase |
48+
| | - Selecting objects based on properties | - Creating a new array with specific calculations |
49+
| **Example (numbers array)** | Filter even numbers | Double each number |
50+
| **Callback function** | `number % 2 === 0` (even) | `number * 2` (double) |
51+
| **Chaining** | Can be chained with `map` for filtering and then transformation | Can be chained with `filter` for additional filtering after transformation |
52+
53+
**Additional Notes:**
54+
55+
- Both `filter` and `map` are non-mutating methods, meaning they don't modify the original array. They create a new array with the filtered or transformed elements.
56+
- While `filter` is generally considered more functional due to its focus on filtering logic, `map` can also be used functionally when the transformations are pure (without side effects).
57+
- When choosing between `filter` and `map`, consider your desired outcome: filtering elements based on a condition (`filter`) or creating a new array with transformed elements (`map`).
58+
3659
# Iteration
3760

3861
1. For Iterating Array

0 commit comments

Comments
 (0)