Javascript Function Argument Passing Function as Parameter to map array

Description

Javascript Function Argument Passing Function as Parameter to map array


var test = [];/*from  w  w w .  j  a va  2 s.c  om*/

function map( numbers, cb ){
  for (var i = 0; i < numbers.length; i++) {
    var newNum = cb( numbers[i] );
    test.push( newNum );
  }
  return test;
}


var numbers = [1,2,3,4,5];
//Produces a new array of values by mapping each value in list through a transformation function
map(numbers, function(num){
  return num * 2; //returns an array of [2,4,6,8,10]
});

console.log( test );



PreviousNext

Related