Useful, Confusing Javascript Array Methods
Javascript provides programmers with a number of helpful array methods, though some are harder to grasp than others. Once understood, these confusing methods prove to be extremely helpful.
The Array.prototype.map() method is extremely useful; .map() is called on an Array object and takes a callback function as an argument. The return value is a new array equal in length to the original array. Each element in the new array is a result of the callback function. For example:
let array = [1, 2, 3, 4];
let newArray = array.map(function(element){
return element * 2;
})newArray
=> [2, 4, 6, 8]
Important things to remember:
—.map() always takes a function as an argument
— .map() will iterate through the original array, performing the callback function on each element
— .map() always returns a new array that is the same length as the original array
— Each element in the new array is the result of the return value of the callback function
Another seemingly confusing Javascript Array method is Array.prototype.filter(). This method also takes a callback function and returns a new array; unlike Array.prototype.map(), however, the callback function usually contains a conditional that checks every element in the original array. If the element passes the conditional test — in other words, if the function returns true — the element will become part of the new array. For example:
let array = [7, 8, 9, 10, 11, 12]
let evens = array.filter(element => {
return element % 2 === 0;
});evens
=>[8, 10, 12]
Important things to remember:
— .filter() always takes a function as an argument
— .filter() will iterate through the original array, performing the callback function on each element
— .filter() returns a new array
— Only if the callback function returns true for an element will that element will be included in the new array
Array.prototype.find() works exactly the same way as .filter(), except .find() returns a single value, the first value which causes the callback function to return true.
let array = [7, 8, 9, 10, 11, 12]
let even = array.find(element => {
return element % 2 === 0;
});even
=>8
Important things to remember:
— .filter() takes a callback function
— .filter() returns one value
— .filter() returns the first element which evaluates to true in the callback function
Get Carly Ann Pecora’s stories in your inbox
Join Medium for free to get updates from this writer.
Array.prototype.reduce() is probably the most confusing of these iterators. The .reduce() method iterates through the array, calling an accumulative function on each element, and returns a single value that is the accumulation/total.
let array = [35, 15, 10]
let result = array.reduce(function(initialValue, currentValue){
return initialValue - currentValue;
})result
=> 10
Important things to note:
— The initialValue, the first argument of the callback function, is the starting point of each iteration; on the first round, intialValue will be 35.
— The currentValue is the element on which the operation will be performed. On the first round, currentValue will be 15.
— On the second round, the initialValue (the starting point of this iteration) will be equal to the total value returned by the first round. The first iteration performed the operation 35 — 15 , and it returned 20 .
— On the second round (the second iteration), initialValue will be 20, and the current value will be 10 , the element on which the accumulation is being performed.
To change the starting point of the accumulation, a second argument can be added to .reduce(). For example:
let array = [8, 7, 6, 5]
let result = array.reduce(function(initialValue, currentValue){
return initialVal + currentVal;
}, 4)result
=>30
Here, .reduce() uses 4 as its initial value. This is valuable in cases when an operation needs to be performed on every single element in an array. For example, if the goal is using .reduce() to add 2 to every element and find the sum of each element + 2 :
let array = [1, 2, 3, 4]//the goal:(1 + 2) + (2 + 2) + (3 + 2) + (4 + 2);
=>18
See what happens when no initialValue is given with .reduce():
let array = [1, 2, 3, 4]
let result = array.reduce(function(initialValue, currentValue){
return initialVal + currentVal + 2;
})result
=> 16
This produces an odd result. If we add 2 to every element and sum the elements, the expected output is 18. But because no initialValue was specified, the above .reduce() method used 1 as its initialValue; taken apart, it looks like this:
1 + (2 + 2) + (3 + 2) + (4 + 2);
=>16With a given initialValue of zero, .reduce() will give the desired result:
let array = [1, 2, 3, 4]
let result = array.reduce(function(initialValue, currentValue){
return initialVal + currentVal + 2;
}, 0)result
=> 18
The initialValue in the above method would be 0 , therefore performing the accumulation on the currentValue1 , producing the desired result.
One important thing to note about all of these iterators is that they are non-destructive. They will each return a new array or value without altering the original array.
Happy coding!
sources:









