Last Updated: February 25, 2016
·
1.18K
· benjycook

Map method for Arrays in javascript

Array.prototype.map = function(func) {
    for(var i = 0; i<this.length;i++) {
        this[i] = func(this[i]);
    }
    return this;
}

Usage example:

a = [1,2,3];
a.map(function(x) {
    return x*2;
})

a is now [2,3,4]

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside