Joe Karlsson
progress. tech. cats. coffee. art. food. photog. mpls.
We have one left...
These methods iterate over the array
Calls a function you provide for each element in the Array
function logTheThings(element, index, array){
console.log('Current element is: ', element);
}
var myArray = [3, 'taco', false, 'Wonka'];
myArray.forEach(logTheThings);
//Current element is: 3
//Current element is: 'taco'
//Current element is: false
//Current element is: 'Wonka'
Your callback function takes 3 params:
var myArray = [3, 'taco', false, 'Wonka'];
myArray.forEach(function(element, index, array) {
console.log('Current element is: ', element);
});
//Current element is: 3
//Current element is: 'taco'
//Current element is: false
//Current element is: 'Wonka'
=>
TIP: If you don't want the function to run against an element return false
Returns true if every element in the Array passes the test you provide
Your callback function takes 3 params:
function isNumeric(element, index, array) {
return !isNaN(parseInt(element));
}
var myArray = [3, 'taco', false, 'Wonka'];
myArray.every(isNumeric); //return false
var myOtherArray = [3, 5, 6, 9];
myArray.every(isNumeric); //return true
Returns true if ANY element in the Array passes the test you provide
Your callback function takes 3 params:
function hazNumber(element, index, array) {
return !isNaN(parseInt(element));
}
var myArray = [3, 'taco', false, 'Wonka'];
myArray.some(hazNumber); //return true, 1 iteration
var myOtherArray = ['test', 'taco', 'hotdog', {}];
myOtherArray.some(hazNumber); //return false, 4 iterations
Returns immediately when first true is found
Creates and returns a new array with all elements that pass true test
Your callback function takes 3 params:
function getNumberz(element, index, array) {
return !isNaN(parseInt(element));
}
var myArray = [3, 'taco', false, 44, 'Wonka'];
myArray.filter(getNumberz); //return [3, 44]
console.log(myArray); //[3, 'taco', false, 44, 'Wonka']
var myOtherArray = ['test', 'taco', 'hotdog', {}];
myOtherArray.filter(getNumberz); //return []
console.log(myOtherArray); //['test', 'taco', 'hotdog', {}]
NOTE: filter() does NOT mutate the original array
Creates/returns a new array with all returned values from callback function
Your callback function takes 3 params:
function tripleIt(element, index, array) {
return element * 3;
}
var myArray = [3, 12, 1, 9];
myArray.map(tripleIt); //return [9, 36, 3, 27]
console.log(myArray) //[3, 12, 1, 9]
var myOtherArray = [3, 12, 1, 'taco'];
myOtherArray.map(tripleIt); //return [9, 36, 3, NaN]
console.log(myArray) //[3, 12, 1, 'taco']
NOTE: map() does NOT mutate the original array
Returns a single value accumulated against all values from callback function
Your callback function takes 4 params and 1 optional param:
function summarize(previousValue, currentValue, index, array) {
return previousValue + currentValue;
}
var myArray = [3, 12, 1, 9];
myArray.reduce(summarize); //return 25
console.log(myArray) //[3, 12, 1, 9]
//Start at a certain initial value
myArray.reduce(summarize, 10); //return 35
console.log(myArray) //[3, 12, 1, 9]
NOTE: reduce() does NOT mutate the original array
If you pass NO optional param, first iteration is at index 1 so that previous/next have values
//Omit index and array is still valid
function summarize(previousValue, currentValue) {
return previousValue + currentValue;
}
var myArray = [3, 12, 1, 9];
myArray.reduce(summarize); //return 25
console.log(myArray) //[3, 12, 1, 9]
//Start at a certain initial value
myArray.reduce(summarize, 10); //return 35
console.log(myArray) //[3, 12, 1, 9]
Returns a single value accumulated against all values from callback function
Your callback function takes 4 params and 1 optional param:
NOTE: reduceRight() does NOT mutate the original array
reduceRight() has the same functionality as reduce() except that it starts from the right side of the input array
By Joe Karlsson