Javascript Array mapper(callback)

Description

Javascript Array mapper(callback)


Array.prototype.mapper = function(callback){
    var arr = [];
    // w  w  w  . jav a2s.  c  o  m
    for (var i = 0; i < this.length; i++)
        callback(this[i]);
        
    return arr;
};




var arr = [1, 2, 3];
Array.prototype.mapper = function(callback){
    for (var i = 0; i < this.length; i++)
        this[i] = callback(this[i]);
};

arr.mapper();
console.log(arr) // -> [2,4,6]



PreviousNext

Related