forEach
(returns defined)map
(returns a new array of the same length)filter
(The result of the callback will ALWAYS be a boolean)some
and every
(The result of the callback will ALWAYS be a boolean)reduce
(whatever returns is stored in accumulator)[].ArrayMethods(function(val, index, array){});
[].reduce(function(accumulator, nextVal, index, array){}, optionParameter);
undefined
Example: Implement my own forEach
function myForEach(arr, callback){
for (var i=0;i<arr.length;i++){
callback(arr[i], i, arr);
}
}
Example:
function halfValues(arr){
var newArr = [];
arr.forEach(function(val){
newArr.push(val/2);
});
return newArr;
}
Example: implement my own map
function myMap(arr, callback){
var newArr=[];
for (var i=0; i<arr.length; i++){
newArr = push(callback(arr[i], i, arr));
}
return newArr;
}
Example: Implementation of filter
function myFilter(arr, callback){
var newArr = [];
for (var i=0; i<arr.length; i++){
if(callback(arr[i], i, array)){
newArr.push(arr[i]);
}
}
return newArr;
}
Example 1
var arr = [1,2,3];
arr.filter(function(value, index, array){
// no need for an if statement, just return an expression that evaluates to true or false!
return value > 2;
});
Example 2
var instructors = [{name: "Elie"},
{name: "Tim"},
{name: "Matt"},
{name: "Colt"}
];
instructors.filter(function(value, index, array){
return value.name.length > 3;
});
Example: Implementation of some
function mySome(arr, callback){
var newArr = [];
for (var i=0; i<arr.length; i++){
if(callback(arr[i], i, array)){
return true;
}
}
return false;
}
Implement my own every
function myEvery(arr, callback){
var newArr = [];
for (var i=0; i<arr.length; i++){
if(callback(arr[i], i, array) === false){
return false;
}
}
return true;
}
function allArrays(arr){
return arr.every(Array.isArray);
}
allArrays([[1], [2], [3,4]]);
allArrays([[1], [2], {}]);
Example
[1,2,3].reduce(function(accumulator, nextvalue, index, array){
//what is returned inside here will be the accumulator value in the next iteration
}, opt2ndParam);
Example: convert array to number
var arr = [1,2,3,4,5];
arr.reduce(function(accumulator, nextValue){
return accumulator + nextValue;
});
accumulator | nextValue | returned value |
---|---|---|
1 | 2 | 3 |
3 | 3 | 6 |
6 | 4 | 10 |
10 | 5 | 15 |
Example: convert array to number (2nd params)
var arr = [1,2,3,4,5];
arr.reduce(function(accumulator, nextValue){
return accumulator + nextValue;
},10);
accumulator | nextValue | returned value |
---|---|---|
10 | 1 | 11 |
11 | 2 | 13 |
13 | 3 | 16 |
16 | 4 | 20 |
20 | 5 | 25 |
Example: convert array to strings (2nd params)
var names = ['Tim', 'Matt', 'Colt', 'Elie'];
names.reduce(function(accumulator, nextValue){
return accumulator += ' ' + nextValue;
},'The instructors are');
accumulator | nextValue | returned value |
---|---|---|
"The instructors are" | "Tim" | "The instructors are Tim" |
"The instructors are Tim" | "Matt" | "The instructors are Tim Matt" |
"The instructors are Tim Matt" | "Colt" | "The instructors are Tim Matt Colt" |
"The instructors are Tim Matt Colt" | "Elie" | "The instructors are Tim Matt Colt Elie" |
Example: convert array to object (2nd params)
var arr = [5,4,1,4,5];
arr.reduce(function(accumulator, nextValue){
if(nextValue in accumulator){
accumulator[nextValue]++;
} else {
accumulator[nextValue] = 1;
}
return accumulator;
},{});
accumulator | nextValue | returned value |
---|---|---|
{} | 5 | {5:1} |
{5:1} | 4 | {5:1, 4:1} |
{5:1, 4:1} | 1 | {5:1, 4:1, 1:1} |
{5:1, 4:1, 1:1} | 4 | {5:1, 4:2, 1:1} |
{5:1, 4:2, 1:1} | 5 | {5:2, 4:2, 1:1} |
Example 1:
function sumOddNumbers(arr){
return arr.reduce(function(accumulator,nextValue){
if(nextValue % 2 !== 0){
accumulator += nextValue;
}
return accumulator;
},0);
}
sumOddNumbers([1,2,3,4,5]);
Example 2:
function createFullName(arr){
return arr.reduce(function(accumulator, nextValue){
accumulator.push(nextValue.first + " " + nextValue.last);
return accumulator;
}, []);
}
createFullName([{first:"Colt", last:"Steele"}, {first:"Matt", last:"Lane"}]);