Javascript Array reduce(f, value)

Description

Javascript Array reduce(f, value)


Array.prototype.reduce = function (f, value) {
  for (var i = 0, len = this.length; i < len; i++) {
    value = f(this[i], value);//from  www.  j  av a 2  s  .  c  om
  }

  return value;
};

var superheroes = ['superman', 'iron man', 'spiderman', 'batman'];

var totalLength = superheroes.reduce(function (elem, acc) {
  return elem.length + acc;
}, 0);

console.log(totalLength);

var numbers = [1,2,3,4,5,6,7,8,9,10];

console.log(numbers.reduce(function (elem, acc) {
  return elem + acc;
}, 0));

console.log(numbers.reduce(function (elem, acc) {
  return elem * acc;
}, 1));

Javascript Array reduce(f,value)


Array.prototype.reduce =function (f,value){
 var i;/*  w w w  . j av a  2 s .c  om*/
 for(i=0; i<this.length; i+= 1){
  value = f(this[i], value);
 }
 return value;
};

//create an array
var data = [4,8,15,16,23,42];

//define two simple functions one will add two
//numbers the other will multyiply two numbers

var add = function(a,b){
 return a+b;
};

var mult = function(a,b){
 return a*b;
};

//invoke the data's reduce method, passing in the
//add function.

var sum = data.reduce(add,0); //sum is 108

//invoke the redce method again, this time passing
//in the multiply function

var product = data.reduce(mult, 1);
//prodct is 741880


//give the data array a total function
data.total = function(){
 return this.reduce(add, 0);
};

total = data.total(); //total is 108



PreviousNext

Related