ã¹ãã¬ããæ§æ(Spread Operator) ...
ã§ä½¿ç¨
for of
ã§åããArray-like(ã¤ãã©ãã«ï¼)ãªãªãã¸ã§ã¯ããåã
ã®å¤ã§å±éã§ãã
â»é
åã§ã®ä»æ§ã¯ES2015/ES6ã§æ¨æºã«ãªã£ã¦ãããããªãã¸ã§ã¯ãã§ã®ä»æ§ã¯ã¾ã ãã©ãã (ECMAScript® 2022 Language Specification)
apply
ãå©ç¨ãã¦é
åãå±éãã¦é¢æ°ã«æ¸¡ãã¦å±
ããããªå¦çãã¹ãã¬ããæ§æã§ã·ã³ãã«ã«æ¸ããã¨ãã§ãã¾ãã
function sum(x, y, z) { return x + y + z } const numbers = [1, 2, 3] console.log( sum(...numbers) ) // 6 console.log( sum.apply(null, numbers) ) // 6
é¢æ°ã®å¼æ°ãã¹ãã¬ããæ§æããã°ãé¢æ°ã«æ¸¡ãããå¼æ°(arguments)ãé
åã¨ãã¦æ±ããã¨ãã§ãã¾ãã(argumentsã¨ã¯éãé
åãªã®ã§ãã®ã¾ã¾map
,reduce
ãªã©ã®é
åæä½ããããã¨ãã§ãã)
function sum(...values) { // valuesã¯é å return values.reduce((sum, val) => { return sum + val }, 0) } console.log( sum(1, 2, 3) ) // 6
é å
é åã®ã³ãã¼
[...Array]
ã§å
ã®é
åãã³ãã¼ããæ°ããé
åãä½æ
const arg1 = [1, 2, 3] // é åã®ã³ãã¼ const arg2 = [...arg1] // [1, 2, 3] // ã³ãã¼ãªã®ã§å ã®é åã¨ã¯å¥ console.log(arg1 === arg2) // false arg2[0] = 0 console.log(arg1, arg2) // [1, 2, 3] [0, 2, 3] // è¦ç´ ã追å ããæ°ããé åãä½æ const arg3 = ['a', ...arg1, 'b'] // ['a', 1, 2, 3, 'b']
é åã®çµå
concat()
ã§çµåãã¦ããã®ãç°¡åã«æ¸ãã
const arg1 = [0, 1, 2] const arg2 = [3, 4, 5] var arg3 = [...arg1, ...arg2] // [0, 1, 2, 3, 4, 5] // â ã¨åç arg3 = arg1.concat(arg2) // [0, 1, 2, 3, 4, 5]
é
åã«è¦ç´ ã追å ããpush()
, unshift()
ã®å¼æ°ã¨ãã¦ã使ãã
var arg1 = [1, 2, 3] var arg2 = ['a', 'b', 'c'] arg1.push(...arg2) // [ 0, 1, 2, 'a', 'b', 'c' ]
var arg1 = [1, 2, 3] var arg2 = ['a', 'b', 'c'] arr1.unshift(...arr2) // [ 'a', 'b', 'c', 0, 1, 2 ]
ãªãã¸ã§ã¯ã
ãªãã¸ã§ã¯ãã®ã³ãã¼
{...Object}
ã§æ°ãããªãã¸ã§ã¯ããä½æ
const obj1 = {a: 1, b: 2} // ãªãã¸ã§ã¯ãã®ã³ãã¼ const obj2 = {...obj1} // { a: 1, b: 2 } console.log(obj2 === obj1) // false obj2.a = 0 console.log(obj1, obj2) // { a: 1, b: 2 } { a: 0, b: 2 } // è¦ç´ ã追å ããæ°ãããªãã¸ã§ã¯ããä½æ const arg3 = {z: 0, ...obj1, c: 3} // { z: 0, a: 1, b: 2, c: 3 }
ãªãã¸ã§ã¯ãã®ãã¼ã¸
é
åã¨åãæ§ã«{...obj, ...obj}
ã§ãªãã¸ã§ã¯ãããã¼ã¸ãããã¨ãã§ãã¾ãã
const obj1 = {a: 1, b: 2} const obj2 = {c: 3, d: 4} const mergedObj = {...obj1, ...obj2} // { a: 1, b: 2, c: 3, d: 4 }
ãªãã¸ã§ã¯ãã«åããã¼ãããå ´åãæå¾ã«è¿½å ãããå¤ãæ®ãã¾ãã
obj = { name: 'Mika', name: 'Aki', name: 'Mikko' } console.log(obj) // { name: 'Mikko' }
åããã¼ãæããªãã¸ã§ã¯ãã®ãã¼ã¸
const obj1 = {a: 1, b: 2} const obj2 = {a: 3, c: 4} const mergedObj = {a: 0, ...obj1, ...obj2, c: 5} // { a: 3, b: 2, c: 5 }
ã¹ãã¬ããæ§æã®æ³¨æç¹
ãªãã¸ã§ã¯ãä¸ã«é åã¯å±éã§ããããé åã«ãªãã¸ã§ã¯ãã¯å±éã§ããªã
ãªãã¸ã§ã¯ãã[...obj]
ã¨é
åä¸ã«ã³ãã¼ãããã¨ããã¨ã¨ã©ã¼ã«ãªãã¾ã
const obj = {a:1, b:2} const arg = [1, ...obj, 2] // TypeError: obj is not iterable
éã«é
åã{...array}
ã§ãªãã¸ã§ã¯ãä¸ã«ã³ãã¼ããã¨ãé
åã®ã¤ã³ããã¯ã¹ããã¼ã¨ãã¦ãªãã¸ã§ã¯ãã«ã³ãã¼ããã¾ãã
const arg = ['foo', 'bar'] const obj1 = {a: 1, ...arg, b: 2} // { '0': 'foo', '1': 'bar', a: 1, b: 2 } // é åã®ã¤ã³ããã¯ã¹ã¨åããã¼ãåå¨ãã¦ããå ´åã¯ããã¼ã¸ããã const obj2 = {0: 'a', ...arg, 2: 'b'} // { '0': 'foo', '1': 'bar', '2': 'b' }
æµ ãã³ãã¼(shallow copy)
ã¹ãã¬ããæ¼ç®å...
ã§ã³ãã¼ããé
åã»ãªãã¸ã§ã¯ãã¯æµ
ãã³ãã¼(shallow copy)ãªã®ã§ããã¹ãããã¦ããé
åã»ãªãã¸ã§ã¯ãã§ã¯æ³¨æãå¿
è¦
é å
const arg = [1, [2, 3], 4] var copy = [...arg] copy[1][0] = 'a' console.log(arg) // [ 1, [ 'a', 3 ], 4 ]
ãªãã¸ã§ã¯ã
const obj = { a: { b: 1 }, c: 2 } var copy = {...obj} copy.a.b = 0 copy.a.d = 3 console.log(obj) // { a: { b: 0, d: 3 }, c: 2 }
ãã¹ãããã¦ããé åã»ãªãã¸ã§ã¯ããæä½ããã¨ãã¨ã®é åã«å½±é¿ãåã¼ãã¦ãã¾ã
null, undefined
iterableã§ã¯ãªãnull
,undefined
ãã¹ãã¬ããæ¼ç®åã§æ±ããã¨ããå ´åãé
å([...null]
, [...undefined]
)ã¯ã¨ã©ã¼ã«ãªããããªãã¸ã§ã¯ã({...null}
, {...undefined}
)ã ã¨ã¨ã©ã¼ã«ãªããªã
[...null] // TypeError: null is not iterable [...undefined] // TypeError: undefined is not iterable {...null} // {} {...undefined} // {}
ã¹ãã¬ããæ§æã®æ´»ç¨
æååãé åã«ãã
æåå(String)ã¯Array-likeãªãªãã¸ã§ã¯ããªã®ã§...str
ã§1æåã¥ã¤é
åã«ãããã¨ãã§ããã
const str = 'ããã«ã¡ãJavaScriptð' console.log([...str]) // ['ã','ã','ã«','ã¡','ã','J','a','v','a','S','c','r','i','p','t','ð' ] const reversed = [...str].reverse().join('') // ðtpircSavaJãã¡ã«ãã
çµµæåã®ãããªãã®ã1æåã¨ãã¦æ±ãããã£ã½ãã
é åã»ãªãã¸ã§ã¯ãã®åå²
åå²ä»£å ¥(Destructuring assignment) æ§æã¯ãé åããå¤ãåãåºãã¦ããããã¯ãªãã¸ã§ã¯ãããããããã£ãåãåºãã¦å¥åã®å¤æ°ã«ä»£å ¥ãããã¨ãå¯è½ã«ãã JavaScript ã®å¼ã§ãã
åºå ¸: 分割代入 - JavaScript | MDN
é å
var [a, b, ...rest] = [1, 2, 3, 4, 5, 6, 7] console.log(a, b, rest) // 1 2 [ 3, 4, 5, 6, 7 ]
ã¹ãã¬ããæ§æã¯"æ®ã"ã§ããæå®ã§ããªã
var [a, b, ...rest, c] = [1, 2, 3, 4, 5, 6, 7] // => SyntaxError: Rest element must be last element
ãªãã¸ã§ã¯ã
ãªãã¸ã§ã¯ãã®åå²ä»£å ¥ã¯å¤æ°åã¨åããã¼ã®å¤ãåå¾ããã
var {a, b} = {a:10, b:20, c:30} console.log(a, b) // 10 20 var {c, d} = {foo:10, bar:20, baz:30} console.log(c, d) // undefined undefined
ã¹ãã¬ããæ§æã§åå²ä»£å ¥ããããã®ã¯key:valueãä¿æãããªãã¸ã§ã¯ãå½¢å¼ã«ãªã
var {a, c, ...rest} = {a:1, b:2, c:3, d:4, e:5} console.log(a, c, rest) // 1 3 { b: 2, d: 4, e: 5 }
âã¿ããã«é¢æ°ã®å¼æ°ã§...
ã使ç¨ãã¦ããã®ãåå²ä»£å
¥ãªã®ããªï¼
function myFunc(x, y, ...other) { ... }
é åã®éè¤ãåé¤
ä¸æãªå¤ãæ ¼ç´ã§ããSet
ãå©ç¨ããã¨é
åããéè¤ãããã¼ã¿ãåãé¤ããé
åãä½æãããã¨ãã§ãã¾ãã
const data = [0, 1, 2, 3.1, true, false, 1, "2", "", 3.1, null, undefined, NaN, false, NaN] const dist = [...new Set(data)] // [ 0, 1, 2, 3.1, true, false, '2', '', null, undefined, NaN ]
new Set()
ãSetãªãã¸ã§ã¯ããè¿ããã®ã[]
ã§é
ååãã¦ããã¨ããèªèã§è¯ãã®ã§ããããï¼
ãªãã¸ã§ã¯ãã®åæå¤
{...null}
, {...undefined}
ã{}
ã«ãªããã¨ãå©ç¨ããã¨ãªãã¸ã§ã¯ãã®ããã©ã«ããªãã·ã§ã³ãã¤ãã£ããã§ãã
const createObj = (options) => { return {name: 'ã¢ã·ãªã', ...options} } obj1 = createObj() // {name: 'ã¢ã·ãªã'} obj2 = createObj({name: 'ä¸æ»èº«ã®æå '}) // {name: 'ä¸æ»èº«ã®æå '}
ããã¯ã£ã½ãã®ã§è¦éããè¯ããã©ããã¯ãã¡ãã£ã¨èãã©ãããããããªã
ã¤ãã¬ã¼ã¿ã¼ãã¡ããã¨ç解ã§ãã¦ãªãã®ã§ã¤ãã©ãã«ã¨Array-likeã®éãã¨ããã¤ãã¤ãã¾ã ãããµããªã¾ã¾ã§ã...
[åè]
- スプレッド構文 - JavaScript | MDN
- JSのスプレッド構文を理解する - Qiita
- 【JavaScript】スプレッド構文の便利な使い方まとめ - Qiita
- JavaScript の イテレータ を極める! - Qiita
- 分割代入 - JavaScript | MDN
- Set - JavaScript | MDN
ãã¼ãã¤ã ç¶å ¥ã 125g
- ã¡ãã£ã¢: é£å&飲æ