There are several ways to iterate over the elements in an array, each with a different purpose.
For example forEach
when you want to do something with every element in the array. It iterates over the array, but doesn't change it.
let myArray = [ 'A', 'B', 'B' ];
myArray.forEach( value => console.log(value) );
console.log(myArray); // Nothing changed: [ 'A', 'B', 'B' ]
Before forEach
was added to Javascript, you could do the same thing like this:
var myArray = [ 'A', 'B', 'B' ];
var i;
for( i = 0 ; i < myArray.length ; i+=1 ) {
console.log( myArray[ i ] );
}
With the new for of-statement the same thing can be done like this:
var myArray = [ 'A', 'B', 'B' ];
for( const value of myArray ) {
console.log( value );
}
Sometimes you want to return a new array based on the values of the first array: You want to map
the values. This returns a new array, based on the first.
let myTable = [ 'Alpha', 'Bravo', 'Charlie' ]
let myArray = [ 2, 1, 0 ]
let myNewArray = myArray.map( value => myTable[value] );
console.log(myNewArray); // [ 'Charlie', 'Bravo', Alpha' ]
Before map
was added to Javascript you could do the same thing like this:
var myTable = [ 'Alpha', 'Bravo', 'Charlie' ]
var myArray = [ 2, 1, 0 ]
var myNewArray = [];
var i;
for ( i = 0; i < myArray.length; i += 1 ) {
myNewArray[ i ] = myTable[ myArray[ i ] ];
}
console.log(myNewArray); // [ 'Charlie', 'Bravo', Alpha' ]
Sometimes you want to filter the values, only keeping some of the values.
let myArray = [ 6, 9, 10, 3, 15, 8, 2 ];
let myNewArray = myArray.filter( value => value >= 10 );
console.log( myNewArray ); // [ 10, 15 ]
Sometimes you wan to aggregate the content of the array, for example to sum all values:
let myArray = [ 6, 9, 10, 3, 15, 8, 2 ];
let sum = myArray.reduce(
(sum, value) => sum + value, // the function
0 // the start value
)
console.log( sum ); // 53
There is also find
that will iterate over all values if the return value is false. The purpose of this is to find a value in the array.
const myArray = [ 6, 9, 10, 3, 15, 8, 2 ];
const myValue = myArray.find( value => value > 10 );
console.log( myValue ); // 15
And then there is some
that iterates over the array until the function returns true. The purpose is to inspect the array to see if at least one value matches. If a match is found it stops the iteration.
const myArray = [ 6, 9, 10, 3, 15, 8, 2 ];
let myResult = myArray.some(
(value, index) => {
console.log('Inspecting index %d with value %d', index, value);
return value >= 10
}
);
console.log( myResult ); // true
And there is every
that only returns true if all values matches the function.
const myArray = [ 6, 9, 10, 3, 15, 8, 2, 0 ];
let myResult = myArray.every(
(value, index) => {
const result = value > 0;
console.log('Inspecting index %d with value %d: %s', index, value, result);
return result;
}
);
console.log( myResult ); // false
In all cases, every element is passed to the function, and you can do whatever you want. But it is bad practices to manipulate the original content of the array with find
or map
, or to use them if you don't want a new array. If you just want to process the values in the array, you use forEach
.