å šéšããã
äžèŠ§
é åãäœæ
[]
new Array()
,Array()
Array.from()
Array.of()
æ°èŠé åãè¿åŽ
Array.prototype.concat()
Array.prototype.filter()
Array.prototype.map()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.slice()
æ€çŽ¢
Array.prototype.every()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.includes()
Array.prototype.indexOf()
Array.prototype.lastIndexOf()
Array.prototype.some()
é å以å€ãè¿åŽ
Array.isArray()
Array.prototype.join()
Array.prototype.toLocaleString()
Array.prototype.toString()
ç Žå£çæäœ
Array.prototype.copyWithin()
Array.prototype.fill()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.unshift()
å埩
Array.prototype.entries()
Array.prototype.forEach()
Array.prototype.keys()
Array.prototype.values()
Array.prototype[Symbol.iterator]()
é åãäœæ
[]
- é ååæåå [] ã®ã²ã¿ã€ãïŒé åãšãããããAdvent Calendar2018 â 09æ¥ç®ïŒ
- ECMAScript® 2018 Language Specification
é åãæ°èŠäœæããèšæ³ãé ååæååã§ããã¡ãœãããããªãã§ããã
[...arr]
ã®ããã«ããŠè€è£œãã§ããŸãã
new Array()
, Array()
- é åã®ã³ã³ã¹ãã©ã¯ã¿ãŒãæ¹ããŠèŠãŠã¿ãïŒé åãšãããããAdvent Calendar2018 â 01æ¥ç®ïŒ
- ECMAScript® 2018 Language Specification
ã³ã³ã¹ãã©ã¯ã¿ãŒãšããŠæ°ããé åãäœæããŸããã¡ãœãããããªãã§ããã
Array
㯠new
ããã£ãŠããªããŠãåãã
Array.from()
- é å颚ãªããžã§ã¯ããArray.from()ã§æ¬ç©ã®é åãžå€æãïŒé åãšãããããAdvent Calendar2018 â 05æ¥ç®ïŒ
- Array.from() | MDN
- ECMAScript® 2018 Language Specification
é åçã®å埩å¯èœãªãªããžã§ã¯ãããé å颚ãªããžã§ã¯ãããé åãäœããŸãã
å埩å¯èœãªãªããžã§ã¯ãã®å Žå㯠[...arr]
ãšã ãããåãã
第2åŒæ°ã®ã³ãŒã«ããã¯é¢æ°ãåããŠãããã§ããã®ãç¹åŸŽã
const obj = { 0: 11, 1: 22, 2: 33, length: 3, }; const arr = Array.from(obj, (value) => 1000 + value); console.log(arr); // [ 1011, 1022, 1033 ]
Array.of()
- Array(3)ã®ä»£ããã«Array.of(3)ã䜿ãããïŒé åãšãããããAdvent Calendar2018 â 03æ¥ç®ïŒ
- Array.of() | MDN
- ECMAScript® 2018 Language Specification
äžããããä»»æåã®åŒæ°ã§é åãäœããŸãã
ã ãããã³ã³ã¹ãã©ã¯ã¿ãŒ new Array()
ãšåãã
ãã ã new Array(3)
ã¯é·ã length
ã 3
ã§ç©ºãæ ã®ã¿ã®é
åãçæããã®ã«å¯Ÿãã Array.of(3)
ã§ããã°æ°å€ãèŠçŽ ãšããŠæ ŒçŽãã [3]
ãäœæã§ããŸãã
çœ åé¿çšã®ãã®ããªã
const arr1 = Array.of(11, 22, 33); console.log(arr1); // [ 11, 22, 33 ] const arr2 = Array.of(11); console.log(arr2); // [ 11 ] console.log(arr2.length); // 1 const arr3 = Array(11); console.log(arr3); // [ <11 empty items> ] console.log(arr3.length); // 11
æ°èŠé åãè¿åŽ
Array.prototype.concat()
é åã«é åãç¹ããŸããéé åã¯ãã ã®èŠçŽ ãšããŠè¿œå ãããŸãã
const arr1 = [1, 2]; const arr2 = [3, 4]; const arr3 = [6, 7]; const arr = arr1.concat(arr2, 5, arr3); console.log(arr); // [ 1, 2, 3, 4, 5, 6, 7 ]
ãããå©çšã㊠flat()
ã¿ãããªããšãã§ããã
const arr = [].concat(...[1, 2, [3, 4]]); console.log(arr); // [ 1, 2, 3, 4 ]
é
å颚ãªããžã§ã¯ãã«ã¯å¯Ÿå¿ããŠããããããããã²ãšã€ã®èŠçŽ ãšããŠè¿œå ãããŸãããã ã [Symbol.IsConcatSpreadable]
ã« true
ãèšå®ãããŠããå Žåã®ã¿ãé
åã®ããã«å±éãããã
const obj1 = { 0: 1, 1: 2, length: 2, }; const obj2 = { 0: 3, 1: 4, length: 2, [Symbol.isConcatSpreadable]: true, }; const arr = [].concat(obj1, obj2); console.log(arr); // [ { '0': 1, '1': 2, length: 2 }, 3, 4 ]
åŒæ°ãäžããããªããŠãæ°èŠé
åãçæããã®ã§ãäžæåã¯é
åãè€è£œããã®ã«ãã䜿ãããŠãŸãããä»ãªã [...arr]
ã§ããã
Array.prototype.filter()
- ãã®åã®éãã®filter()ã§çµã蟌ã¿ãïŒé åãšãããããAdvent Calendar2018 â 12æ¥ç®ïŒ
- Array.prototype.filter() | MDN
- ECMAScript® 2018 Language Specification
ã³ãŒã«ããã¯é¢æ°ã§æ¡ä»¶å€æã㊠true
ãè¿ããèŠçŽ ã ãã®æ°èŠé
åãè¿ããŸãã
const activeItems = items.filter((v) => v.active);
Array.prototype.map()
èŠçŽ ããšã«ã³ãŒã«ããã¯é¢æ°ãåŒã³ãããããè¿ãå€ãè©°ããæ°èŠé åãè¿ããŸãã
const arr1 = [11, 22, 33]; const arr2 = arr1.map((v) => 1000 + v); console.log(arr2); // [ 1011, 1022, 1033 ]
ã¡ããŒäŸ¿å©ãåäœã®èšäºæžãããã©æ ã足ããªãã£ãã®ã§æªå ¬éã
Array.prototype.reduce()
- reduce()ã¯Arrayã«ãŠæ匷âŠâŠããŒããŠãããïŒé åãšãããããAdvent Calendar2018 â 23æ¥ç®ïŒ
- Array.prototype.reduce() | MDN
- ECMAScript® 2018 Language Specification
ç¹°ãè¿ããªããä»»æã®æ»ãå€ãçšæã§ããã
const items = [ { id: '101', name: 'Apple' }, { id: '102', name: 'Banana' }, { id: '103', name: 'Orange' }, ]; const map = items.reduce((acc, record) => { acc[record.id] = record; return acc; }, {}); console.log(map); // { '101': { id: '101', name: 'Apple' }, // '102': { id: '102', name: 'Banana' }, // '103': { id: '103', name: 'Orange' } }
ãªãã§ãã§ããæ匷ã®é¢æ°ã ãã©ãèªã¿ã¥ãããä»ã®æžãæ¹ãã§ãããªããã£ã¡ã䜿ããã
Array.prototype.reduceRight()
reduce()
ã¯å
é ããæ«å°ŸãŸã§ç¹°ãè¿ãã®ã«å¯Ÿããéã«æ«å°Ÿããå
é ãŸã§ç¹°ãè¿ããã€ã
const arr = [11, 22, 33]; const arr2 = arr.reduceRight((acc, v) => { acc.push(v); return acc; }, []); console.log(arr2); // [ 33, 22, 11 ]
Array.prototype.slice()
é åã®äžéšãæãåºããŠè¿ããŸããåŒæ°ã«éå§äœçœ®ãçµäºäœçœ®ãäžããŸãã
const csvData = [ ['date', 'value', 'note'], ['2018-12-01', 11, 'Apple is nice'], ['2018-12-02', 22, 'Banana is nice'], ['2018-12-03', 33, 'Orange is nice'], ]; const actualData = csvData.slice(1); console.log(actualData); // [ [ '2018-12-01', 11, 'Apple is nice' ], // [ '2018-12-02', 22, 'Banana is nice' ], // [ '2018-12-03', 33, 'Orange is nice' ] ]
çç¥ãããšéå§äœçœ®ã¯å é ãçµäºäœçœ®ã¯æ«å°Ÿã«ãªããŸãã
ããããè² æ°ãäžããããšã§ãæ«å°Ÿããæ°ããäœçœ®ãšããããšãã§ããŸãã
ã¡ãªã¿ã«ç©ºãæ ããã£ãŠãããŸãåãã
æ€çŽ¢
Array.prototype.every()
å šãŠã®èŠçŽ ãæ¡ä»¶ã«åèŽããã調ã¹ãçåœå€ãè¿ããŸãã
const arr = [11, 22, 33]; const result = arr.every((v) => v > 10); console.log(result); // true
const arr = [11, 22, 33]; const result = arr.every((v) => v % 2); console.log(result); // false
ã²ãšã€ã§ãåèŽããã°ããšããå Žå㯠some()
ãã
æ¡ä»¶ã«åèŽããèŠçŽ ãåŸããå Žå㯠filter()
ãã
Array.prototype.find()
- é åããæ¡ä»¶ã«åããã®ã¯find()ã§èŠã€ããããïŒé åãšãããããAdvent Calendar2018 â 04æ¥ç®ïŒ
- Array.prototype.find() | MDN
- ECMAScript® 2018 Language Specification
æ¡ä»¶ã«åèŽããèŠçŽ ãæ¢ããæåã®ãã®è¿ããŸããèŠã€ãããªãå Žå㯠undefined
ã
const arr = [11, 22, 33]; const result = arr.find((v) => v > 15); console.log(result); // 22
æ¡ä»¶ã«åèŽããäœçœ®ãå¿
èŠãªå Žå㯠findIndex()
ãã
æ¡ä»¶ã«åèŽãããã®å
šãŠãå¿
èŠãªå Žå㯠filter()
ãã
æ¡ä»¶ã«åèŽãããã©ããã ãå¿
èŠãªå Žå㯠some()
ãã
Array.prototype.findIndex()
æ¡ä»¶ã«åèŽããèŠçŽ ãæ¢ããæåã®ãã®ã®äœçœ®è¿ããŸããèŠã€ãããªãå Žå㯠-1
ã
const arr = [11, 22, 33]; const result = arr.findIndex((v) => v > 15); console.log(result); // 1
æ¡ä»¶ããã ç¹å®ã®ãã®ãšçãããã©ããã®å Žå㯠indexOf()
ãã
æ¡ä»¶ã«åèŽããèŠçŽ èªäœãå¿
èŠãªå Žå㯠find()
ãã
Array.prototype.includes()
æå®ã®ãã®ãèŠçŽ ã«å«ããã©ãã調ã¹ãçåœå€ãè¿ããŸãã第2åŒæ°ã«æ€çŽ¢éå§äœçœ®ãæå®ããããšãã§ããŸãã
const arr = [11, 22, 33]; const result = arr.includes(22); console.log(result); // true
NaN
ãæ€ç¥ã§ããŸãã
console.log([NaN].includes(NaN)); // true
æ¡ä»¶ã«åèŽãããã®ã®æç¡ã調ã¹ãå Žå㯠some()
ãã
æå®ã®ãã®ã®äœçœ®ãå¿
èŠãªå Žå㯠indexOf()
ãã
Array.prototype.indexOf()
æå®ã®ãã®ãèŠçŽ ããæ¢ããæåã®ãã®ã®äœçœ®è¿ããŸããèŠã€ãããªãå Žå㯠-1
ã
const arr = [11, 22, 33]; const result = arr.indexOf(22); console.log(result); // 1
NaN
ã¯èŠã€ããããŸããã
console.log([NaN].indexOf(NaN)); // -1
æç¡ã ããããã°è¯ããªã includes()
ãã
åå€ã«éããæ¡ä»¶ã§æ€çŽ¢ãããªã findIndex()
ãã
æ«å°Ÿããæ€çŽ¢ããå Žå㯠lastIndexOf()
ãã
Array.prototype.lastIndexOf()
indexOf()
ã¯å
é ããæ€çŽ¢ããã®ã«å¯Ÿããæ«å°Ÿããæ€çŽ¢ããŸãã第2åŒæ°ã«æ€çŽ¢éå§äœçœ®ãæå®ããããšãã§ããŸãã
const arr = [11, 22, 33, 44, 55, 22]; const result = arr.lastIndexOf(22, 1); console.log(result); // 1
æ€çŽ¢éå§äœçœ®ã¯åŸãããäœçªç®ããæããŸããè² æ°ãäžãããšãéã«å é ããæ°ããŸãã
æ»ãå€ã¯å é ããã®äœçœ®ã«ãªããŸãã
å
é ããæ€çŽ¢ããå Žå㯠indexOf()
ãã
Array.prototype.some()
ããããã®èŠçŽ ãæ¡ä»¶ãæºãããã©ãã調ã¹ãçåœå€ãè¿ããŸãã
const arr = [11, 22, 33]; const result = arr.some((v) => v > 15); console.log(result); // true
ç¹å®ã®å€ãå«ããã©ãã調ã¹ãå Žå㯠includes()
ãã
èŠçŽ ãåºçŸããäœçœ®ãå¿
èŠãªå Žå㯠indexOf()
ãã
é å以å€ãè¿åŽ
Array.isArray()
äžãããããªããžã§ã¯ããé åãã©ãããå€æããŠçåœå€ãè¿ããŸãã
instanceof
ãšç°ãªã prototype
ãè¿œãã®ã§ã¯ãªããã€ã³ã¹ã¿ã³ã¹ãå
éšã®ãé
åç¹æ®ãªããžã§ã¯ã (Array exotic object)ãïŒãããã¯ãã® Proxy
ïŒã§ãããã©ãããå€æããŸãã
class SugoiArray extends Array {} const sugoiArray = new SugoiArray(); console.log(sugoiArray instanceof Array); // true console.log(Array.isArray(sugoiArray)); // true const sugokunaiArray = Object.create([]); console.log(sugokunaiArray instanceof Array); // true console.log(Array.isArray(sugokunaiArray)); // false
å©ç¹ã¯ <iframe>
çãå©çšããŠå¥ãŠã£ã³ããŠã® Array
ã³ã³ã¹ãã©ã¯ã¿ãŒããçæãããã€ã³ã¹ã¿ã³ã¹ã§ããå Žåã«ãæ£ç¢ºã«å€å®ã§ããç¹ã§ããïŒ window.Array
ãš elFrame.contentWindow.Array
ãå¥ã®ãããïŒ
Node.jsãªã vm
ãè¡ãæ¥ããé
åã€ã³ã¹ã¿ã³ã¹ã«ã€ããŠåãããšãèšããŸãã
const vm = require('vm'); const sandbox = {}; vm.createContext(sandbox); vm.runInContext('arr = [11, 22, 33]', sandbox); const { arr } = sandbox; console.log(arr); // [ 11, 22, 33 ] console.log(arr instanceof Array); // false console.log(Array.isArray(arr)); // true
Array.prototype.join()
èŠçŽ ãæå®ã®æååãæã¿ã€ã€é£çµããè¿ããŸãã
const arr = [2018, 12, 24]; const result = arr.join('-'); console.log(result); // '2018-12-24'
èŠçŽ ã¯æååãžå€æãããŸããäŸå€ãšã㊠undefined
ãš null
ã¯ç©ºæåå ''
ã«ãªããŸãã
é£çµåã«ç¬èªã«å€æãããå Žå㯠map()
ãçµã¿åãããŠã
Array.prototype.toLocaleString()
å©çšè ã®èšèªèšå®ã«åãããæååãžå€æãããã®ãè¿ããŸãã “local” ã§ã¯ãªã “locale” ã
MDNãã転èŒãæå
ã®ChromeãšFirefoxã§ã¯åããããNode.js v10ã§ã¯ toString()
ã¿ãããªåäœã«ãªããŸããã
const prices = ['ï¿¥7', 500, 8123, 12]; const result = prices.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }); console.log(result); // ï¿¥7,ï¿¥500,ï¿¥8,123,ï¿¥12
ãããŸãç¥ããŸãããã¡ãœããã®å¯Ÿå¿ãšã¯å¥ã«åèšèªã®å¯Ÿå¿ãããã®ã§ãå©çšå¯èœãã©ããã®èª¿æ»ã倧å€ããã
åºæ¬çã«ã¯å¥ã®ä»æ§ã«ãªã£ãŠããããã§ãã
Array.prototype.toString()
æååãžå€æããŠè¿ããŸããå
éšã§ã¯åŒæ°ãªãã® join()
ãåŒãã§ããŸãã
const arr = [11, 22, 33]; const result = arr.toString(); console.log(result); // '11,22,33'
ç Žå£çæäœ
Array.prototype.copyWithin()
察象ã®é åã®èŠçŽ ãããã®é åäžã®å¥ã®äœçœ®ãžè€è£œããŸããç Žå£çå€æŽã䌎ããŸãã
const arr = [11, 22, 33, 44, 55]; const arr1 = arr.copyWithin(2); console.log(arr1); // [ 11, 22, 11, 22, 33 ]
åäœã®é°å²æ°ã¯ãããªæããïŒãã®ãŸãŸã ãšã ãã¶ä»æ§ã«è¶³ããªããã©ãïŒ
const copyWithin = (arr, target = 0, start = 0, end = arr.length) => { for (let i = 0; i < end - start; i++) { arr[target + i] = arr[start + i]; } };
ãããªã®ãã£ããã ããã¶ã TypedArray
ã§ãã€ãããŒã¿ã¿ãããªã®ãããããïœã£ãŠãããšãã«é«éã§æçšãªãã ãšæãã
Array.prototype.fill()
é åã®èŠçŽ ãæå®å€ã§äžæžãããŸããç Žå£çå€æŽã䌎ããŸãã第2ã第3åŒæ°ã§ç¯å²ãæå®ã§ããŸãã
const arr = [11, 22, 33]; arr.fill(0); console.log(arr); // [ 0, 0, 0 ]
空ãæ ã«ãªã£ãŠãããã®ãåããŸãã
const arr = new Array(3); console.log(arr); // [ <3 empty items> ] arr.fill(); console.log(arr); // [ undefined, undefined, undefined ]
ä»»æåã®èŠçŽ æ°ãæã€é åãäœæããã®ã«äœ¿ãããããããªãã
Array.prototype.pop()
æ«å°Ÿã®èŠçŽ ãé åããåé€ããè¿ããŸããç Žå£çå€æŽã䌎ããŸãã
const arr = [11, 22, 33]; const result = arr.pop(); console.log(arr); // [ 11, 22 ] console.log(result); // 33
éã«è¿œå ããå Žå㯠push()
ãã
å
é ããååŸããå Žå㯠unshift()
ãã
Array.prototype.push()
é åã®æ«å°Ÿãžæå®ã®é ç®ãè¿œå ããŸããç Žå£çå€æŽã䌎ããŸãã
const arr = [11, 22, 33]; const result = arr.push(99); console.log(arr); // [ 11, 22, 33, 99 ] console.log(result); // 4
è¿œå åŸã® length
ãè¿ããŸãã
éã«ååŸããå Žå㯠pop()
ãã
å
é ãžè¿œå ããå Žå㯠shift()
ãã
Array.prototype.reverse()
èŠçŽ ã®é åºãå転ãããŸããç Žå£çå€æŽã䌎ããŸãã
const arr = [11, 22, 33]; arr.reverse(); console.log(arr); // [ 33, 22, 11 ]
ç Žå£ããããªãå Žåã [...arr].reverse()
ã®ããã«è€è£œãäœã£ãŠããå転ãããšè¯ãã§ãããã
Array.prototype.shift()
å é ã®èŠçŽ ãé åããåé€ããè¿ããŸããç Žå£çå€æŽã䌎ããŸãã
const arr = [11, 22, 33]; const result = arr.shift(); console.log(arr); // [ 22, 33 ] console.log(result); // 11
éã«è¿œå ããå Žå㯠unshift()
ãã
æ«å°ŸããååŸããå Žå㯠pop()
ãã
Array.prototype.sort()
æå®ã®æ¯èŒé¢æ°ãçšããŠäžŠã¹æ¿ããŸããç Žå£çå€æŽã䌎ããŸãã
const arr = [94, 39, 63, 87, 52, 3, 10, 95, 5]; arr.sort((n, m) => n - m); console.log(arr); // [ 3, 5, 10, 39, 52, 63, 87, 94, 95 ]
ã¡ãªã¿ã«æ¯èŒé¢æ°ã®æ»ãå€ã¯çåœå€ãããªããã
é¢æ°ãäžããªãå Žåãæååã«å€æããŠæ¯èŒããŸããïŒå€æã¯æ¯èŒçšã«è¡ãããã®ã¿ã§ãçµæã¯å ã®æ°å€ã®ãŸãŸãïŒ
const arr = [94, 39, 63, 87, 52, 3, 10, 95, 5]; arr.sort(); console.log(arr); // [ 10, 3, 39, 5, 52, 63, 87, 94, 95 ]
çœ ã§ããã
ããã²ãšã€çœ ããã£ãŠãããã¯å®å®ãœãŒããä¿èšŒãããŸãããã€ãŸãå·®ããªãèŠçŽ ã®é åºãå€æŽããåŸãã
ä»æ§æžã«ã¯ä»¥äžã®ããã«ããã
The sort is not necessarily stable
ããšãå
ã®é
åãç Žå£ããããªãå Žåã [...arr].sort()
ã®ããã«è€è£œãäœã£ãŠããå転ãããšè¯ãã§ãããã
Array.prototype.splice()
- é åã®èŠçŽ åé€ã¯splice()ã§ãïŒé åãšãããããAdvent Calendar2018 â 06æ¥ç®ïŒ
- é åã®èŠçŽ æ¿å ¥ã眮ãæããsplice()ã§ãVue.jsã§ã倧äžå€«ãïŒé åãšãããããAdvent Calendar2018 â 07æ¥ç®ïŒ
- Array.prototype.splice() | MDN
- ECMAScript® 2018 Language Specification
èŠçŽ ã®äžéšãåé€ããããæ°ããé ç®ãæ¿å ¥ãããããŸããç Žå£çå€æŽã䌎ããŸãã
const arr = [11, 22, 33, 44, 55]; const result1 = arr.splice(1, 1); console.log(arr); // [ 11, 33, 44, 55 ] console.log(result1); // [ 22 ] const result2 = arr.splice(1, 2, 99, 88, 77); console.log(arr); // [ 11, 99, 88, 77, 55 ] console.log(result2); // [ 33, 44 ]
åãé€ããèŠçŽ ãé åã«è©°ããŠè¿ããŸãã
å
šåé€ããå Žåã¯ã splice()
ã䜿ãããšã arr.length = 0
ã§ãããŸããïŒVue.jsã§ã¯æŽæ°ã®ã¿ã€ãã³ã°ã«åé¡ãããã®ã§ splice()
ã䜿ããŸããïŒ
Array.prototype.unshift()
é åã®æ«å°Ÿãžæå®ã®é ç®ãè¿œå ããŸããç Žå£çå€æŽã䌎ããŸãã
const arr = [11, 22, 33]; const result = arr.unshift(99); console.log(arr); // [ 99, 11, 22, 33 ] console.log(result); // 4
è¿œå åŸã® length
ãè¿ããŸãã
éã«ååŸããå Žå㯠shift()
ãã
æ«å°Ÿãžè¿œå ããå Žå㯠push()
ãã
å埩
Array.prototype.entries()
åèŠçŽ ã®ã€ã³ããã¯ã¹ãšèŠçŽ ã®çµãé åã§åŸãããå埩åãè¿ããŸãã
const arr = [11, 22, 33]; for (const [key, value] of arr.entries()) { console.log(key, value); } // 0 11 // 1 22 // 2 33
ä»»æã®ãªããžã§ã¯ãã®ããããã£åãããããã£å€ã®çµã§å埩ããå¿
èŠãããå Žå㯠Object.entries()
ãã
Array.prototype.forEach()
- ã ãããã®ç¹°ãè¿ãã¯é åã®forEach()ã§ããããïŒé åãšãããããAdvent Calendar2018 â 17æ¥ç®ïŒ
- Array.prototype.forEach() | MDN
- ECMAScript® 2018 Language Specification
ç¹°ãè¿ããŸããåçŽãª for
æã§ããã°ãããã«çœ®ãæãå¯èœã§ãã
const arr = [11, 22, 33]; arr.forEach((value, index, originalArray) => { console.log(value, index, originalArray); }); // 11 0 [ 11, 22, 33 ] // 22 1 [ 11, 22, 33 ] // 33 2 [ 11, 22, 33 ]
æ»ãå€ã¯ãããŸãããïŒåžžã« undefined
ã§ããïŒ
ä»ã®å€ãã®ç¹°ãè¿ãç³»ã¡ãœãããããã§ãããèŠçŽ ãååšããªãå Žåã¯åŒã°ããŸããã
const arr = [, 22,, 44, 55]; arr.forEach((value, index) => { console.log(`[${index}]`, value); }); // [1] 22 // [3] 44 // [4] 55
è€éãªç¹°ãè¿ãã空ãæ ãžã®å¯Ÿå¿ãå¿
èŠãªå Žåã¯ãä»ã®ã¡ãœãããšçµã¿åããããã for
æãã
é
å颚ãªããžã§ã¯ããç¹°ãè¿ãå Žåã¯ã [...obj]
ã§é
åãžå€æããŠãã forEach()
ãåŒã³ãŸãã
ãããã¯DOMã® NodeList
çãèåãªé
å颚ãªããžã§ã¯ãã¯ç¬èªã« forEach()
ãæã£ãŠãããããããŸãããïŒ forEach()
ã®èšäºãåç
§ãïŒ
Array.prototype.keys()
åèŠçŽ ã®ã€ã³ããã¯ã¹ãåŸãããå埩åãè¿ããŸãã
const arr = [11, 22, 33]; for (const key of arr.keys()) { console.log(key, arr[key]); } // 0 11 // 1 22 // 2 33
ä»»æã®ãªããžã§ã¯ãã®ããããã£åã§å埩ããå¿
èŠãããå Žå㯠Object.keys()
ãã
Array.prototype.values()
åèŠçŽ ãåŸãããå埩åãè¿ããŸãã
const arr = [11, 22, 33]; for (const value of arr.values()) { console.log(value); } // 11 // 22 // 33
ä»»æã®ãªããžã§ã¯ãã®ããããã£å€ã§å埩ããå¿
èŠãããå Žå㯠Object.values()
ãã
Array.prototype[Symbol.iterator]()
- å埩åŠçã®äžèº«ãIterableãšIteratorã«ã€ããŠä»æ§æžã調ã¹ãŠã¿ããïŒé åãšãããããAdvent Calendar2018 â 19æ¥ç®ïŒ
- Array.prototype[@@iterator]() | MDN
- ECMAScript® 2018 Language Specification
å埩åãè¿ããŸãããã®ã¡ãœããã®ååšãé åãå埩å¯èœããããããã®ã§ãã
for-of
ã®å
éšåŠçã§åŒã°ããŸãã
const arr = [11, 22, 33]; arr[Symbol.iterator] = function* () { console.log('Called!', this); }; for (const element of arr) { console.log(element); // â1床ãå®è¡ãããªã } // 'Called!' [ 11, 22, 33, [Symbol(Symbol.iterator)]: [GeneratorFunction] ]
äžèº«ã¯ values()
ã§ãã
const arr = []; console.log(arr[Symbol.iterator] === arr.values); // true
ãã®ä»
flat()
, flatMap()
- Array.prototype.flat() | MDN
- Array.prototype.flatMap() | MDN
- Array.prototype.flatMap & Array.prototype.flat
[[1], [2, 3]]
ã [1, 2, 3]
ãžéããã®ãšãããã« map()
æ©èœãã»ããã«ãªã£ããåŸçã
ECMAScript 2018ã®ä»æ§ã«ã¯å ¥ã£ãŠãªãä»æ§æ¡ãæ¬çš¿å·ççŸæç¹ã§ã¯ “Stage 3 Draft” ã ããã§ãã
Edge以å€ã§ã¯ããåãã
ãããŸã
ãšããããã§æ¬ç©ã®æ¹ã®ã¢ããã³ãã«ã¬ã³ããŒãå šéšéããããä»å¹Žã®ããããã¢ããã³ãã«ã¬ã³ããŒããããŸãã§ãã
æ¯æ¥ã²ãšã€é åã®ã¡ãœããã玹ä»ãããããã®æãã§èããŠããã ãã©ãæ°ãä»ãã°ä»æ§æžããã¡ãã¡ã€ãŸã¿èªã¿ããŠããŸã£ãã
ããã¯æ¥œããã£ãã§ããèªãã§ãã ãã£ãæ¹ã¯ããããšãããããŸãããæ¥å¹Žãããããã
ãšã£ãŽãã±ããã®ã·ãã
åè
现ãããšãã¯å šéšæ¬æäžã«ããããããã§èš±ããŠâŠâŠã