ES 6拡張演算子(3点演算子)…の使い方
1183 ワード
まとめてES 6拡張演算子(3点演算子)…の使い方を記録します.
一、操作対象
展開
展開
一、操作対象
展開
let user = { name:' ' , age:28 }
let userInfo = {
...user,
description:'info'
}
// {name: " ", age: 28, description: "info"}
統合let user = { name:' ' , age:28 }
let info = { description:'info' }
let userInfo = {...user,...info}
// {name: " ", age: 28, description: "info"}
開発:Object.assignを利用しても実現できます.let user = { name:' ' , age:28 }
let info = { description:'info' }
let userInfo = Object.assign(user,info)
// {name: " ", age: 28, description: "info"}
構成を解くlet userInfo = { name:' ' , age:28 , description:'info' }
let { name , ...other } = userInfo
// {age: 28, description: "info"}
二、操作配列展開
let arr = [1,2,3]
let newArr = [0 , ...arr , 5]
// [0, 1, 2, 3, 5]
統合let arr = [1,2,3]
let newArr = [4,5]
console.log([...arr,...newArr])
// [1, 2, 3, 4, 5]
構成を解くlet arr = [1,2,3,4,5]
let [a,b,...other] = arr
// 1 2 [3, 4, 5]