首页 > 编程语言 > 详细

数组解构赋值

时间:2020-01-18 23:41:56      阅读:75      评论:0      收藏:0      [点我收藏+]
//数组解构
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)

数组解构赋值

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!