.forEach()
hobbies.join(', ')
..addEventListener()
is a higher order function.Array.forEach()
An array method that loops through each item and invokes callback(item)
at each iteration.
const moods = ['happy', 'sad', 'excited'];
const callback = function(item) {
console.log(item);
}
moods.forEach(callback);
// happy
// sad
// excited
item
is passed as an argument to the callback
.Many looping methods will optionally pass the current array index
to the callback
.
const moods = ['happy', 'sad', 'excited'];
const callback = function(item, index) {
console.log(index);
}
moods.forEach(callback);
// 0
// 1
// 2
item
and index
can have a name of your choice.const moods = ['happy', 'sad', 'excited'];
// Create an empty accumulator variable
let output = '';
// Create a callback that adds each list item to `output`
const handleItem = function(item) {
output += `<li>${item}</li>`;
}
// Invoke the callback on each array item
moods.forEach(handleItem);
// Print the output to the page as an unordered list
document.querySelector('body').innerHTML = `<ul>${output}</ul>`;
.forEach()
return
keyword in your callback
will not do anything;for
vs Array.forEach()
Using for
:
for (let i = 0; i < hobbies.length; i++) {
console.log(hobbies[i]);
}
Using Array.forEach()
:
moods.forEach(function(item) {
console.log(item);
});
Most implementations of higher order functions use anonymous callback functions.
for
when there's no array to loop though;for
is a valid way to loop through an array, it's prone to bugs:// This loop runs one too many times
for (let i = 0; i <= hobbies.length; i++) {
// last loop: hobbies[i] is undefined
console.log(hobbies[i]);
}
Array.forEach()
;for
(there's no String.forEach()
).Array.filter()
Loops through an array and returns an array of filtered elements.
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const handleFilter = function(word) {
if (word.length > 6) {
// word is added to result
return true;
} else {
// word is NOT added to result
return false;
}
}
const result = words.filter(handleFilter);
// expected output: Array ["exuberant", "destruction", "present"]
Before:
const handleFilter = function(word) {
if (word.length > 6) {
return true;
} else {
return false;
}
}
const result = words.filter(handleFilter);
After:
const result = words.filter(word => word.length > 6);
These examples have the same output.
The if
/else
statements are redundant:
const handleFilter = function(word) {
if (word.length > 6) {
return true;
} else {
return false;
}
}
After:
const handleFilter = function(word) {
return word.length > 6;
}
Anonymous functions are often used in industry:
const handleFilter = function(word) {
return word.length > 6;
}
const result = words.filter(handleFilter);
After:
const result = words.filter(function(word) {
return word.length > 6;
});
Using ES6 fat arrow syntax:
const result = words.filter(word => {
return word.length > 6;
});
When the body of a function can be put on one line, the {}
and return
can be omitted:
const result = words.filter(function(word) {
return word.length > 6;
});
After:
const result = words.filter(word => word.length > 6);
These examples will use some of the previous shortcuts.
Array.map()
Loops through an array and returns a new array containing items returned at each iteration of the loop.
const numbers = [1, 4, 9, 16];
// pass a function to map that squares each item
const numberSquared = array.map(function(number) {
return number * 2;
});
console.log(numberSquared);
// expected output: Array [2, 8, 18, 32]
Array.find()
Returns the first element that satisfies the provided testing function. Otherwise, undefined
is returned.
const numbers = [5, 12, 8, 130, 44];
const foundNumber = numbers.find(function(number) {
return number > 10;
});
console.log(foundNumber);
// expected output: 12
true
;.filter()
, .map()
and .find()
.filter()
/.map()
returns and Arrayfind()
returns an Array itemtrue
or false
.filter()
/.map()
: adds item to result on true
;find()
: returns the current item when true and quits the loop.Array.forEach()
Array.filter()
Array.map()
Array.find()