Baseline Widely available
map()
㯠Array
ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ãä¸ãããã颿°ãé
åã®ãã¹ã¦ã®è¦ç´ ã«å¯¾ãã¦å¼ã³åºãããã®çµæãããªãæ°ããé
åãçæãã¾ãã
const array = [1, 4, 9, 16];
// 颿°ã map ã«æ¸¡ã
const mapped = array.map((x) => x * 2);
console.log(mapped);
// äºæ³ãããçµæ: Array [2, 8, 18, 32]
æ§æ
map(callbackFn)
map(callbackFn, thisArg)
弿°
callbackFn
é åã®ããããã®è¦ç´ ã«å¯¾ãã¦å®è¡ãã颿°ããã®è¿å¤ã¯ãæ°ããé åã®åä¸ã®è¦ç´ ã¨ãã¦è¿½å ããã¾ãããã®é¢æ°ã¯ã以ä¸ã®å¼æ°ã§å¼ã³åºããã¾ãã
element
é åå ã§ç¾å¨å¦çä¸ã®è¦ç´ ã§ãã
index
ç¾å¨å¦çä¸ã®è¦ç´ ã®é åå ã«ãããæ·»åã§ãã
array
map()
ãå¼ã³åºãããé
åã§ãã
thisArg
çç¥å¯
callbackFn
ãå®è¡ããã¨ãã« this
ã¨ãã¦ä½¿ãå¤ã§ããå復å¦çã¡ã½ãããåç
§ãã¦ãã ããã
ä¸ãããã颿°ãé åã®ãã¹ã¦ã®è¦ç´ ã«å¯¾ãã¦å¼ã³åºãããã®çµæãããªãæ°ããé åã§ãã
解説map()
ã¡ã½ããã¯å復å¦çã¡ã½ããã§ããæå®ããã颿° callbackFn
ãé
åã«å«ã¾ããåè¦ç´ ã«å¯¾ãã¦ä¸åº¦ãã¤å¼ã³åºãããã®çµæããæ°ããé
åãæ§ç¯ãã¾ãããããã®ã¡ã½ãããä¸è¬çã«ã©ã®ããã«åä½ããã®ãã«ã¤ãã¦ã®è©³ç´°ã¯ãå復å¦çã¡ã½ããã®ç¯ãã覧ä¸ããã
callbackFn
ã¯å¤ãå²ãå½ã¦ããã¦ããé
åã¤ã³ããã¯ã¹ã«å¯¾ãã¦ã®ã¿å¼ã³åºããã¾ããçé
åã§ç©ºã®ã¹ãããã«å¯¾ãã¦ã¯å¼ã³åºããã¾ããã
map()
ã¡ã½ããã¯æ±ç¨çã§ãããã㯠this
å¤ã« length
ããããã£ã¨æ´æ°ãã¼ã®ããããã£ããããã¨ã ããæå¾
ãã¾ãã
map
ã¯æ°ããé
åã使ããã®ã§ãè¿ãããé
åã使ããªãå ´åãmap ã使ãã®ã¯ãã¿ã¼ã³ã«åãã¾ããã代ããã« forEach
ã¾ã㯠for...of
ã使ç¨ãã¦ãã ããã
次ã®ã³ã¼ãã¯ãæ°å¤ãããªãé åãåãããããã®æ°å¤ã®å¹³æ¹æ ¹ãããªãæ°ããé åãçæãã¾ãã
const numbers = [1, 4, 9];
const roots = numbers.map((num) => Math.sqrt(num));
// roots ã®å
容㯠[1, 2, 3] ã¨ãªã
// numbers ã®å
容㯠[1, 4, 9] ã®ã¾ã¾
map ã使ç¨ãã¦é
åå
ã®ãªãã¸ã§ã¯ããåãã©ã¼ããã
次ã®ã³ã¼ãã¯ããªãã¸ã§ã¯ãã®é åãåãåããæ°ãã«ãã©ã¼ããããããæ°ããé åãçæãã¦ãã¾ãã
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 }
// ]
parseInt() ã map() ã§ä½¿ç¨
ã³ã¼ã«ããã¯ã¯ã1 ã¤ã®å¼æ°ï¼èµ°æ»ä¸ã®è¦ç´ ï¼ã¨ã¨ãã«ä½¿ç¨ããã®ãä¸è¬çã§ããä¸é¨ã®é¢æ°ã¯ã追å ã®ãªãã·ã§ã³å¼æ°ãåã£ã¦ãã1 ã¤ã®å¼æ°ã¨ã¨ãã«ä½¿ç¨ããããã¨ãããããã¾ãããã®ãããªç¿æ £ã¯ãæ··ä¹±ãæãåä½ã«ã¤ãªããå¯è½æ§ãããã¾ããæ¬¡ã®ä¾ãèãã¦ã¿ã¾ãããã
["1", "2", "3"].map(parseInt);
è¿å¤ã¯ [1, 2, 3]
ã¨ãªãããã§ãããå®éã«ã¯ [1, NaN, NaN]
ã¨ãªãã¾ãã
parseInt
ã¯å¤§æµä¸ã¤ã®å¼æ°ã®ã¿ã§ä½¿ããã¾ãããå®éã«ã¯ 2 ã¤ã®å¼æ°ãåã£ã¦ãã¾ãã 1 ã¤ç®ã¯æ°å¤æååã 2 ã¤ç®ã¯åºæ°ã§ãã Array.prototype.map
ã¯ã³ã¼ã«ããã¯ã«ãè¦ç´ ãã¤ã³ããã¯ã¹ãé
åã® 3 ã¤ã®å¼æ°ãä¸ãã¦ãã¾ãã parseInt
㯠3 ã¤ç®ã®å¼æ°ãç¡è¦ãã¾ããã 2 ã¤ç®ã®å¼æ°ã¯ç¡è¦ãã¾ãããããã¯æ··ä¹±ãèµ·ããå¯è½æ§ãããã½ã¼ã¹ã§ãã
ç¹°ãè¿ãæé ã®æ£ç¢ºãªä¾ã¯ä»¥ä¸ã®éãã§ãã
/* æåã®å復å¦ç ï¼ã¤ã³ããã¯ã¹ã¯ 0ï¼: */ parseInt("1", 0); // 1
/* 2 ã¤ç®ã®å復å¦çï¼ã¤ã³ããã¯ã¹ã¯ 1ï¼: */ parseInt("2", 1); // NaN
/* 3 ã¤ç®ã®å復å¦çï¼ã¤ã³ããã¯ã¹ã¯ 2ï¼: */ parseInt("3", 2); // NaN
ããã解決ããã«ã¯ã弿°ã 1 ã¤ã ãåãå¥ã®é¢æ°ãå®ç¾©ãã¾ãã
["1", "2", "3"].map((str) => parseInt(str, 10)); // [1, 2, 3]
ã¾ãã弿°ã 1 ã¤ã ãåã Number
颿°ã使ç¨ãããã¨ãã§ãã¾ãã
["1", "2", "3"].map(Number); // [1, 2, 3]
// parseInt() ã¨ã¯éã£ã¦ã Number() 㯠float ã¾ã㯠(解決ãã) ææ°è¡¨ç¾ãè¿ãã¾ãã
["1.1", "2.2e2", "3e300"].map(Number); // [1.1, 220, 3e+300]
// æ¯è¼ã®ããã«ãä¸è¨ã®é
åã« parseInt() ãç¨ããã¨æ¬¡ã®ããã«ãªãã¾ãã
["1.1", "2.2e2", "3e300"].map((str) => parseInt(str, 10)); // [1, 2, 3]
詳ãã説æã¯ã A JavaScript optional argument hazard (Allen Wirfs-Brock) ãåç §ãã¦ãã ããã
undefined ãæã¤å¯¾å¿ä»ããããé åundefined
ãè¿ãããããã¾ãã¯ä½ãè¿ãããªãå ´åãçµæã®é
åã«ã¯ undefined
ãå«ã¾ãã¾ããè¦ç´ ãåé¤ãããå ´åã¯ã filter()
ã¡ã½ããããã§ã¼ã³ãããã flatMap()
ã¡ã½ããã使ç¨ãã¦ãåé¤ã示ã空ã®é
åãè¿ãã¾ãã
const numbers = [1, 2, 3, 4];
const filteredNumbers = numbers.map((num, index) => {
if (index < 3) {
return num;
}
});
// index 㯠0 ããå§ã¾ãã®ã§ã filterNumbers 㯠1,2,3 ããã³ undefined ã«ãªãã¾ãã
// filteredNumbers 㯠[1, 2, 3, undefined]
// numbers 㯠[1, 2, 3, 4] ã®ã¾ã¾
å¯ä½ç¨ã®ãã対å¿ä»ã
ã³ã¼ã«ããã¯ã«ã¯å¯ä½ç¨ãããã¾ãã
const cart = [5, 15, 25];
let total = 0;
const withTax = cart.map((cost) => {
total += cost;
return cost * 1.2;
});
console.log(withTax); // [6, 18, 30]
console.log(total); // 45
ãªããªããã³ãã¼ã¡ã½ããã¯ç´ç²é¢æ°ã¨ã¨ãã«ä½¿ç¨ããã®ãæé©ã ããã§ãããã®å ´åãé åã 2 åå復å¦çãããã¨ãé¸ã¹ã¾ãã
const cart = [5, 15, 25];
const total = cart.reduce((acc, cost) => acc + cost, 0);
const withTax = cart.map((cost) => cost * 1.2);
ãã®ãã¿ã¼ã³ã極端ã«ãªãã map()
ãè¡ãæçãªãã¨ãå¯ä½ç¨ãçºçããããã¨ã ãã«ãªã£ã¦ãã¾ããã¨ãããã¾ãã
const products = [
{ name: "sports car" },
{ name: "laptop" },
{ name: "phone" },
];
products.map((product) => {
product.price = 100;
});
ååè¿°ã¹ãããã«ãããã¯ãã¿ã¼ã³ã«åãã¾ãã map()
ã®è¿å¤ã使ç¨ããªãå ´åã¯ã代ããã« forEach()
ã¾ã㯠for...of
ã«ã¼ãã使ç¨ãã¦ãã ããã
products.forEach((product) => {
product.price = 100;
});
ãããã¯ã代ããã«æ°ããé åã使ãããã¨ãã§ãã¾ãã
const productsWithPrice = products.map((product) => ({
...product,
price: 100,
}));
callbackFn ã®ç¬¬ 3 弿°ã®ä½¿ç¨
array
弿°ã¯ãé
åã®å¥ã®è¦ç´ ã«ã¢ã¯ã»ã¹ãããå ´åãç¹ã«ããã®é
åãåç
§ããæ¢åã®å¤æ°ããªãå ´åã«ä¾¿å©ã§ããæ¬¡ã®ä¾ã§ã¯ãã¾ã filter()
ã使ç¨ãã¦æ£ã®å¤ãæ½åºããæ¬¡ã« map()
ã使ç¨ãã¦ãããããã®è¦ç´ ã飿¥ããè¦ç´ ã¨ãã®èªèº«ã®å¹³åå¤ã§ããæ°ããé
åã使ãã¦ãã¾ãã
const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6];
const averaged = numbers
.filter((num) => num > 0)
.map((num, idx, arr) => {
// arr 弿°ããªãå ´åã夿°ã«ä¿åããã«ä¸éé
åã«
// ç°¡åã«ã¢ã¯ã»ã¹ããæ¹æ³ã¯ãªã
const prev = arr[idx - 1];
const next = arr[idx + 1];
let count = 1;
let total = num;
if (prev !== undefined) {
count++;
total += prev;
}
if (next !== undefined) {
count++;
total += next;
}
const average = total / count;
// å°æ°ç¹ä»¥ä¸ 2 æ¡ãä¿æãã
return Math.round(average * 100) / 100;
});
console.log(averaged); // [2, 2.67, 2, 3.33, 5, 5.33, 5.67, 4]
array
弿°ã¯ãæ§ç¯ä¸ã®é
åã§ã¯ããã¾ãããã³ã¼ã«ããã¯é¢æ°ããæ§ç¯ä¸ã®é
åã«ã¢ã¯ã»ã¹ããæ¹æ³ã¯ããã¾ããã
çé
å㯠map()
ã®å¾ãçé
åã®ã¾ã¾ã§ãã空ã®ã¹ãããã®ã¤ã³ããã¯ã¹ã¯è¿ãããé
åã«ããã¦ã空ã®ã¾ã¾ã§ãããã³ã¼ã«ããã¯é¢æ°ãå¼ã³åºããããã¨ã¯ããã¾ããã
console.log(
[1, , 3].map((x, index) => {
console.log(`Visit ${index}`);
return x * 2;
}),
);
// Visit 0
// Visit 2
// [2, empty, 6]
é
å以å¤ã®ãªãã¸ã§ã¯ãã«å¯¾ãã map() ã®å¼ã³åºã
map()
ã¡ã½ãã㯠this
ã® length
ããããã£ãèªã¿è¾¼ã¿ã次ã«ãã¼ã length
ããå°ããéè² ã®æ´æ°ã§ããåããããã£ã«ã¢ã¯ã»ã¹ãã¾ãã
const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 4,
3: 5, // length ã 3 ãªã®ã§ map() 空ã¯ç¡è¦ããã
};
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);
ãã£ã¨ç°¡åãªæ¹æ³ã¯ Array.from()
ã¡ã½ããã使ç¨ãããã¨ã§ãã
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4