| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map | [Back] [Original] |
Get to know MDN better
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]
map(callbackFn)
map(callbackFn, thisArg)
map() this callbackFn callbackFn
const numbers = [1, 4, 9];
const roots = numbers.map((num) => Math.sqrt(num));
// roots [1, 2, 3]
// numbers [1, 4, 9]
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
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() this length
const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 4,
};
console.log(Array.prototype.map.call(arrayLike, (x) => x ** 2));
// [ 4, 9, 16 ]
querySelectorAll querySelectorAll NodeList
option
const elems = document.querySelectorAll("select option:checked");
const values = Array.prototype.map.call(elems, ({ value }) => value);
map()
console.log(
[1, , 3].map((x, index) => {
console.log(`Visit ${index}`);
return x * 2;
}),
);
// Visit 0
// Visit 2
// [2, empty, 6]
["1", "2", "3"].map(parseInt);
[1, 2, 3], [1, NaN, NaN].
parseInt Array.prototype.map parseInt map 3
// 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
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
const strings = ["10", "10", "10"];
const numbers = strings.map(parseInt);
console.log(numbers);
// [10, NaN, 2]
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 |
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()Array.prototype.findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()Array.prototype.toReversed()Array.prototype.toSorted()Array.prototype.toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()Array.prototype.with()Array.prototype[Symbol.iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()| Web Proxy Viewer | New URL | Original Page |