Javascript Array Iterate Reduce








Iterative Methods

Javascript array has five iterative methods.

  • every() - Runs a function on every item and returns true if the function returns true for every item.
  • filter() - Runs a function on every item and returns an array of all items for which the function returns true.
  • forEach() - Runs a function on every item . No return value.
  • map() - Runs a function on every item and returns the result in an array.
  • some() - Runs a function on every item and returns true if the function returns true for any one item.

Each of the methods accepts two arguments:

  • a function to run
  • an optional scope object in which to run the function changing the value of this.

The function passed into will receive three arguments:

  • the array item value,
  • the position of the item in the array, and
  • the array object itself.

These methods do not change the values contained in the array.


var numbers = [1,2,3,4,5,4,3,2,1];
//from  ww w . j a  va2 s .c o  m
var everyResult = numbers.every(function(item, index, array){
    return (item > 2);
});
console.log(everyResult);       //false

var someResult = numbers.some(function(item, index, array){
    return (item > 2);
});
console.log(someResult);       //true

var filterResult = numbers.filter(function(item, index, array){
    return (item > 2);
 });

console.log(filterResult);   //[3,4,5,4,3]

var mapResult = numbers.map(function(item, index, array){
    return item * 2;
 });

console.log(mapResult);   //[2,4,6,8,10,8,6,4,2]

numbers.forEach(function(item, index, array){
    console.log(item);
    console.log(index);
});

The code above generates the following result.





Reduction Methods

Reduction methods iterate over all array items and return one value.

Javascript has two reduction methods:

  • reduce() starts at the first item and continues to the last
  • reduceRight() starts at the last and continues to the first.

They both accept two arguments:

  • a function to call on each item and
  • an optional initial value upon which the reduction is based.

The function passed into reduce() or reduceRight() accepts four arguments:

  • the previous value,
  • the current value,
  • the item's index, and
  • the array object.

Value returned from the function is passed in as the first argument for the next item.





Reduction Methods Example

You can use the reduce() and reduceRight() method to perform operations such as adding all numbers in an array.


var values = [1,2,3,4,5];
var sum = values.reduce(function(prev, cur, index, array){
   return prev + cur;/* w  w w.j  a  va2  s. c  o  m*/
});
console.log(sum); //15

var sum = values.reduceRight(function(prev, cur, index, array){
    return prev + cur;
});
console.log(sum); //15

In the code above when calling reduce(), the first time the callback function is executed, prev is 1 and cur is 2; the second time, prev is 3 resulting of adding 1 and 2, and cur is 3 which is the third item in the array.

The first iteration occurs on the second item in the array, so the first argument is the first item in the array and the second argument is the second item in the array.

The code above generates the following result.