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

Array.prototype.map() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.map()

20157

map()

const array1 = [1, 4, 9, 16];

// Pass a function to map
const map1 = array1.map((x) => x * 2);

console.log(map1);
// Expected output: Array [2, 8, 18, 32]

js
map(callbackFn)
map(callbackFn, thisArg)

callbackFn

element

index

array

map()

thisArg

callbackFn this

map() callbackFn

callbackFn

map() this callbackFn callbackFn

  • map() callbackFn
  • callbackFn
  • callbackFn callbackFn

map() this length

map forEach for...of

js
const numbers = [1, 4, 9];
const roots = numbers.map((num) => Math.sqrt(num));

// roots      [1, 2, 3]
// numbers    [1, 4, 9]

map

js
const kvArray = [
  { key: 1, value: 10 },
  { key: 2, value: 20 },
  { key: 3, value: 30 },
];

const reformattedArray = kvArray.map(({ key, value }) => ({ [key]: value }));

console.log(reformattedArray); // [{ 1: 10 }, { 2: 20 }, { 3: 30 }]
console.log(kvArray);
// [
//   { key: 1, value: 10 },
//   { key: 2, value: 20 },
//   { key: 3, value: 30 }
// ]

map map

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

console.log(doubles); // [2, 8, 18]
console.log(numbers); // [1, 4, 9]

map()

map() this length

js
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
};
console.log(Array.prototype.map.call(arrayLike, (x) => x ** 2));
// [ 4, 9, 16 ]

NodeList map()

querySelectorAll querySelectorAll NodeList

option

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

Array.from()

map()

map()

js
console.log(
  [1, , 3].map((x, index) => {
    console.log(`Visit ${index}`);
    return x * 2;
  }),
);
// Visit 0
// Visit 2
// [2, empty, 6]

parseInt() map()

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

[1, 2, 3], [1, NaN, NaN].

parseInt Array.prototype.map parseInt map 3

parseInt

js
// parseInt(string, radix) -> map(parseInt(value, index))
/*  (index  0): */ parseInt("1", 0); // 1
/*  (index  1): */ parseInt("2", 1); // NaN
/*  (index  2): */ parseInt("3", 2); // NaN

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

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

// 
["1", "2", "3"].map((str) => parseInt(str)); // [1, 2, 3]

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

//  parseInt() Number() 
["1.1", "2.2e2", "3e300"].map(Number); // [1.1, 220, 3e+300]

//  parseInt():
["1.1", "2.2e2", "3e300"].map((str) => parseInt(str)); // [1, 2, 3]

parseInt map

js
const strings = ["10", "10", "10"];
const numbers = strings.map(parseInt);

console.log(numbers);
// [10, NaN, 2] 

undefined

undefined

js
const numbers = [1, 2, 3, 4];
const filteredNumbers = numbers.map((num, index) => {
  if (index < 3) {
    return num;
  }
});

// index  0  filterNumbers  123  undefined
// filteredNumbers  [1, 2, 3, undefined]
// numbers  [1, 2, 3, 4]

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.map


Web Proxy Viewer  |  New URL  |  Original Page