Array.prototype.map()
map()
- Calls one same provided callback function once for each element in original array and constructs a new array from the results.
- In other word: The new array consists of the callback function result on each of the element.
- Just think the new array is a map image of the original.
-
map(...)
- Syntax
-
// Callback function map(callbackFn) map(callbackFn, thisArg) // Inline callback function map(function (element) {...}) map(function (element, index) {...}) map(function (element, index, array) {...}) map(function (element, index, array) {...}, thisArg) // Arrow function map((element) => {...}) map((element, index) => {...}) map((element, index, array) => {...})
-
- Parameters (2):
-
callbackFn
: The callback functionthisArg
(optional) : Value to use asthis
when running callback function.undefined
is assigned if not provided.
- Return value:
- New array with each element being the result of the callback function.
- Original array:
- In general: not changed.
- Unless manipulated during the map-callback function run, through access to the original array using array and index parameters. This is generally avoided, except in some special cases.
- Callback function
-
function callbackFn(element, index, array) { ... return new element value; } // Element is pushed to the new array. It will occupy the same index.
-
- Parameters (3):
-
element
: Current element being processindex
(optional): index of current element being processarray
(optional): The original array
- Return values:
-
- New element value for the new array.
- Map function is invoked only for cells with assigned values, including
undefined
. - It is not called for missing elements of the array.
-
- Indexes that have never been set
- Indexes which have been deleted
empty
cell?- It's included in the new array, but map function is not called. I guess, it could have been merely "nothing is put in that cell" and the next element will go to the subsequent index.
-
const array = [1, "a", , undefined, null, NaN, true, false]; const map1 = array.map(el => el); const map2 = array.map(el => !el); const map3 = array.map(el => "cat"); array; // (8) [1, 'a', empty, undefined, null, NaN, true, false] map1; // (8) [1, 'a', empty, undefined, null, NaN, true, false] map2; // (8) [false, false, empty, true, true, true, false, true] map3; // (8) ['cat', 'cat', empty, 'cat', 'cat', 'cat', 'cat', 'cat'] let counter = 0; const map4 = array.map(el => {counter++; return el}); counter; // 7 map4; // (8) ['z', 'z', empty, 'z', 'z', 'z', 'z', 'z']
- See that
empty
is included in the result list, but map function is not called. Evidence: empty spot, and counter is only 7. - Let's compare with similar operations in filter.
-
const array = [1, "a", , undefined, null, NaN, true, false]; const filter1 = array.filter(el => el); const filter2 = array.filter(el => !el); const filter3 = array.filter(el => el || !el); array; // (8) [1, 'a', empty, undefined, null, NaN, true, false] filter1; // (3) [1, 'a', true] filter2; // (4) [undefined, null, NaN, false] filter3; // (7) [1, 'a', undefined, null, NaN, true, false] let counter = 0; const filter4 = array.filter(el => {counter++; return el || !el}); counter; // 7 filter4; // (7) [1, 'a', undefined, null, NaN, true, false]
- See that the function is not called for empty cell either. Counter is also only 7. Also in the result array, at most we get 7 elements.
- When to not use
map()
? - When we are not going to use the new array
- Example of good use:
- When the new array is needed as an input for other function/request with certain value/format.
- When array is to be sent out to a third party and they don't need / should not see the complete information.
- Example of good use:
- What actually happen when
map()
is called -
- Very similar to
filter()
except it creates new array of new values according to callback function. - When
map
is called, it sees the cells that are there. Only to those cells, the callback function will be called.- New future cells created during the callback function run will be ignored
- If content is shifted or changed, then whatever new value populate the cell will be used.
- It doesn't call the callback function when the cell is empty or not existing. But even when the callback function is not called, the cell with the same index is already assigned in the new array. They will be left empty as well.
- Cells that are empty because the value is never assigned. This cells marked as
empty
in the array.- Remember,
undefined
is an assigned value. - As well as
null
andNaN
. Well,NaN
's type is actuallynumber
- Remember,
- Cells that are empty upon deletion (like with
delete
operator). - Cells that are removed after
map()
is called, for example bypop()
method from callback function.
So theseempty
cells are mappedempty
too in the new array. - Cells that are empty because the value is never assigned. This cells marked as
- For each cell, callback function is called. Current cell value of the original array is used. This could be different from original array value when
map()
is initiated, if previous cell run changed the value of the consequent one.Callback function results in a value. This value is added to the new array cell with the same index. - New array has the same length as the original array. Each cell value is transformed from the original cell value of the same index. Empty stays empty.
- In the end, we have 2 array. Original and map result.
- Very similar to