Skip to content

Commit 5fb2473

Browse files
authored
Merge pull request #40 from gmihov001/patch-13
Explained filter method
2 parents c2b620b + e87dbf7 commit 5fb2473

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed
Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
# `12` Delete element
22

3-
The only way to delete `Daniella` from the array (without cheating) will be to create a new list with all the other people but Daniella.
3+
One of the ways to delete Daniella from the array (without cheating) will be to create a new list with all the other people but Daniella.
44

5-
## 📝Instructions:
5+
That happens to be the default behavior of the `Array.filter()` method which you should know. Similar to the `.forEach()` and `.map()` methods, it is a higher-order function, which means that it calls another function to achieve its goals. That secondary **callback** function is called by the `.filter()` with up to three parameters (optional) and the return can only be one thing - a condition:
6+
7+
```js
8+
9+
(elementBeingIterated, indexOfThatElement, theIteratedArray) => condition;
10+
11+
```
12+
13+
So if you want to keep only the numbers 2 and 4 from an array of numbers, your filter method would look like this:
14+
15+
```js
16+
let array = [2, 9, 5, 6, 4, 1, 2, 3, 4];
17+
18+
let newArray = array.filter((element) => element === 2 || element === 4);
19+
20+
console.log(newArray); // outcome is [2, 4, 2, 4]
21+
22+
```
23+
The `.filter()` method automatically creates a new array in which only the elements that pass the condition are kept. Any other elements are dropped from the newArray.
24+
25+
You can learn more about this method [here](https://www.w3schools.com/jsref/jsref_filter.asp)
26+
27+
28+
### Instructions:
29+
30+
1. Please create a `deletePerson` function that deletes any given person from an array and returns a new array without that person.
631

7-
1. Please create a `deletePerson` function that deletes any given person from the array and returns a new array without that person.
832

933
### Expected result:
1034

1135
```js
1236

13-
['juan', 'ana', 'michelle', 'stefany', 'lucy', 'barak']
14-
['ana', 'michelle', 'daniella', 'stefany', 'lucy', 'barak']
37+
['juan', 'ana', 'michelle', 'stefany', 'lucy', 'barak', 'emilio']
38+
['ana', 'michelle', 'daniella', 'stefany', 'lucy', 'barak', 'emilio']
1539
['juan', 'ana', 'michelle', 'daniella', 'stefany', 'lucy', 'barak']
16-
```
40+
```

0 commit comments

Comments
 (0)