[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/map [Back]  [Original]

Array.prototype.map() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.prototype.map()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

map() .

In this article

const new_array = arr.map(function callback( currentValue[, index[, array]]) {
    //    new_array
}[, thisArg])

callback

, arr. , callback , new_array.

callback, , :

currentValue

.

index

.

array

, .

thisArg

. , this callback

, callback .

map callback , . callback , , undefined. ( , , .

callback : , , .

map thisArg, callback this. this undefined. , this, callback, this, .

map , ( callback ).

, map, callback. , map, callback. callback, , , , map ; .

:

, .

js
const numbers = [1, 4, 9];
const roots = numbers.map(Math.sqrt);
//  roots  [1, 2, 3],  numbers    [1, 4, 9]

: ,

, , . , map .

js
const numbers = [1, 4, 9];
const doubles = numbers.map((num) => num * 2);
//  doubles  [2, 8, 18],  numbers    [1, 4, 9]

: map

, map String ASCII, :

js
const map = Array.prototype.map;
const charCodes = map.call("Hello World", (x) => x.charCodeAt(0));
//  charCodes  [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

: map querySelectorAll

, , querySelectorAll. :

js
const elems = document.querySelectorAll("select option:checked");
const values = Array.prototype.map.call(elems, ({ value }) => value);

Array.from().

: map

js
const string = "12345";
const reversed = Array.prototype.map
  .call(string, (x) => x)
  .reverse()
  .join("");
// reversed  '54321'
// :  '==='   ,    

String.split() (. split()).

:

( )

- (, ). , . .

js
//  :
["1", "2", "3"].map(parseInt);
//      [1, 2, 3],
//    [1, NaN, NaN]

//  parseInt     ,    .
//    ,   -   .
//   callback Array.prototype.map  3 :
// ,     .
//    parseInt,   , ,
//  .       .

const returnInt = (element) => parseInt(element, 10);

["1", "2", "3"].map(returnInt);
//     (  )

//         " !?":
["1", "2", "3"].map(Number); // [1, 2, 3]

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.map


Web Proxy Viewer  |  New URL  |  Original Page