|
1 | 1 | # `12` Delete element |
2 | 2 |
|
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. |
4 | 4 |
|
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. |
6 | 31 |
|
7 | | -1. Please create a `deletePerson` function that deletes any given person from the array and returns a new array without that person. |
8 | 32 |
|
9 | 33 | ### Expected result: |
10 | 34 |
|
11 | 35 | ```js |
12 | 36 |
|
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'] |
15 | 39 | ['juan', 'ana', 'michelle', 'daniella', 'stefany', 'lucy', 'barak'] |
16 | | -``` |
| 40 | +``` |
0 commit comments