数组解构赋值

Posted qjb2404

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数组解构赋值相关的知识,希望对你有一定的参考价值。

//数组解构
let arr = [1,2,3,4,5,6,7]
let [one,two] = arr
let [one, , ,four] = arr
console.log(one,two,four)

let arr = ‘abcd‘
let [first, ,third] = arr
console.log(first,third)

let [first, ,third] = new Set([1,2,5,6,7])
console.log(first,third)

let user = { name:‘s‘,surname:‘t‘};
[user.name,user.surname] = [1,2]
console.log(user)

//循环解构
let user = { name:‘s‘,surname:‘t‘};
for( let [k,v] of Object.entries(user)){
   console.log(k ,v)
}

//剩余变量解构
let arr = [1,2,3,4,5,6,7,8,9]
let [first,second,...last] = arr
console.log(first,second,last)

//当数据为空时,解构赋值为undefined
let arr = []
let [first,second,...last] = arr
//可以设置默认值
let [first=‘a‘,second,...last] = arr
console.log(first,second,last)

以上是关于数组解构赋值的主要内容,如果未能解决你的问题,请参考以下文章

数组的解构赋值

ES6数组的解构赋值( 下)

3-变量的解构赋值

2.变量的解构赋值

数组的解构赋值

数组的解构赋值