JS: Array.prototype.map

By Xah Lee. Date: . Last updated: .
arrayX.map(f)
Apply function to every element of the array. Return the new array. Original array is not changed.
The function f is passed 3 args: • current_elementcurrent_indexarrayX.
arrayX.map(f, thisArg)
Use thisArg for this Binding of f. If it is not given, undefined is used.
const a1 = [3,4,5];
const a2 = a1.map( (x => (x+1)) );

console.log(a2);  // [ 4, 5, 6 ]

// original is not changed
console.log(a1);  // [ 3, 4, 5 ]

Example of using second argument:

// example of using map with second argument
function ff (x) { return [ x , this ] }
console.log( [3,4,5].map( ff , 9 ) );
// [ [ 3, 9 ], [ 4, 9 ], [ 5, 9 ] ]
JS in Depth
XAH
Buy Xah JavaScript Tutorial
JS in Depth
XAH

JS Obj Ref

DOM


JS Obj Ref

Array

prototype