- Parameters
array
and index
of callback function
- Used to access and modify the original array and elements while filtering.
-
filter(callbackFn)
function callbackFn(element, index, array) {...}
filter(function (element, index, array) {...})
filter((element, index, array) => {...})
- Some examples. These codes can be added to the body of the callback function.
- Appending new elements:
array.push(newEl);
- Deleting elements:
array.pop();
- Modifying current elements:
array[index] += string
- Modifying next elements:
array[index+1] += string
- Important: In using this note, remember to always re-entering the original array. Since we are playing with modification of original array.
- Modifying element while filtering: toLowerCase
- Given: array of animals
-
const animals = ['VELOCIRAPTOR', 'DODO', 'GREAT AUK', 'OX', 'THYLACINE', 'DOG'];
- Task:
- Filter short name animals (max 6 characters).
- Lowercase the content as we go.
-
const filterLC = animals.filter((animal, idx, arr) => {
arr[idx] = arr[idx].toLowerCase();
return animal.length <= 6;
});
- Results:
-
filterLC;
animals;
- Note: For each test to the array's cell, the array's cell value is recorded first to be passed to the new array in the case test returns true. This happen even before the codeblock is run. Modification didn't affect this value.
- Modifying element while filtering: add text
- Given: array of animals
-
const animals = ['velociraptor', 'dodo', 'great auk', 'ox', 'thylacine', 'dog'];
- Task:
- Filter short name animals (max 6 characters).
- Add "My " in the beginning of each element.
-
const filterMy = animals.filter((animal, idx, arr) => {
arr[idx] = "My " + animal;
return animal.length <= 6;
});
- Results:
-
filterMy;
animals;
- Note: Even though the value spitted out to the new array is taken in the beginning of each cell test, the value itself can be processed and taken account into the filter condition.
-
const filterMy2 = animals.filter((animal, idx, arr) => {
arr[idx] = "My " + animal;
return arr[idx].length <= 6;
});
- Results:
-
filterMy2;
animals;
- Modifying "the next" element while filtering: add text
- Given: array of animals
-
const animals = ['velociraptor', 'dodo', 'great auk', 'ox', 'thylacine', 'dog'];
- Task:
- Filter short name animals (max 6 characters).
- Add "My " in the beginning of "next" element.
-
const filterNext = animals.filter((animal, idx, arr) => {
arr[idx+1] = "My " + arr[idx+1];
return animal.length <= 6;
});
- Results:
-
filterNext;
animals;
- Note:
- Remember: Cell initial value is taken and this value is to be included in the new result array in the case test is positive.
- So for the first cell, the value will be the original value.
- However the value of second cell is changed during the first cell test run. Therefore, by the time the second cell test is started, the cell value has changed. Therefore this new value is what going to be included in the new array in the case test result is true.
- Filter function counting the length of array and/or creating the pointer to each cell to later be tested in the beginning of the run. New cells created after that point will not even get tested.
-
const filterNext2 = animals.filter((animal, idx, arr) => {
arr[idx+1] = "My " + arr[idx+1];
return animal.length > 6;
});
- Results:
-
filterNext2;
animals;
- See that the newly generated element cell doesn't get tested.
- Appending elements
- Given: array of animals
-
const animals = ['velociraptor', 'dodo', 'great auk', 'ox', 'thylacine', 'dog'];
- Task:
- Filter short name animals (max 6 characters).
- Add new elements as we go
-
const filterAppend = animals.filter((animal, idx, arr) => {
arr.push("new");
return animal.length <= 6;
});
- Results:
-
filterAppend;
animals;
- Note: Newly created element cells don't get tested.
- Appending elements in between
- Given: array of animals
-
const animals = ['velociraptor', 'dodo', 'great auk', 'ox', 'thylacine', 'dog'];
- Task:
- Filter short name animals (max 6 characters).
- Add new elements "in next cell" as we go
-
const filterAppend = animals.filter((animal, idx, arr) => {
arr.splice(idx + 1, 0, "new");
return animal.length <= 6;
});
- Results:
-
filterAppend;
animals;
- Note: See the originally existing cells get tested. That it's "new", only in our mind it is new element. What actually happens in array is, the new one is the cell. Contents are merely assigned, reassigned, or deleted.
- Let's see similar but asking for more than 6 characters
-
const filterAppend = animals.filter((animal, idx, arr) => {
arr.splice(idx + 1, 0, "new");
return animal.length > 6;
});
- Results:
-
filterAppend;
animals;
- Removing elements
- Given: array of animals
-
const animals = ['velociraptor', 'dodo', 'great auk', 'ox', 'thylacine', 'dog'];
- Task:
- Filter short name animals (max 6 characters).
- Remove elements as we go
-
const filterRemove = animals.filter((animal, idx, arr) => {
arr.pop();
return animal.length <= 6;
});
- Results:
-
filterRemove;
animals;
- It never reached ox, let alone dog. Because by the time the test of cell 3 is done, cell 4, 5, and 6 are already gone.
- Deleting next element as filter go
- Given: array of animals
-
const animals = ['velociraptor', 'dodo', 'great auk', 'ox', 'dog', 'thylacine'];
- Task:
- Filter short name animals (max 6 characters).
- Delete next elements as we go. Deletion works differently from popping. Deletion only delete the content, it doesn't remove the cell. When a test reaching an empty cell, it skipped it entirely. Not merely failed, the code is not run at all.
-
const filterDel = animals.filter((animal, idx, arr) => {
delete arr[idx + 1];
return animal.length <= 6;
});
- Results:
-
filterDel;
animals;
- Cell 2, 4, and 6 is not even run. Evident is, "greak auk" and "dog" didn't get erased. The whole test for those cells are skipped.