码迷,mamicode.com
首页 > 编程语言 > 详细

数组解构赋值

时间:2020-01-19 00:03:31      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:fir   abc   undefined   数据   one   name   变量   trie   log   

//数组解构
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)

数组解构赋值

标签:fir   abc   undefined   数据   one   name   变量   trie   log   

原文地址:https://www.cnblogs.com/qjb2404/p/12210652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!