网站首页 > 技术文章 正文
原文地址:https://dev.to/bhagatparwinder/destructuring-arrays-1dkf
解构或者解构赋值是一个让我们可以对数组或对象进行拆包,然后把它们赋值给变量的语法。这篇文章将重温一下数组解构。
为了演示,我们一起来看一个例子。我们将创建一个函数然后接受数字数组并打印那些数字。
const myNumbers = (arrOfNumbers) => {
const a = arrOfNumbers[0];
const b = arrOfNumbers[1];
const c = arrOfNumbers[2];
const d = arrOfNumbers[3];
const e = arrOfNumbers[4];
const f = arrOfNumbers[5];
const g = arrOfNumbers[6];
console.log(a, b, c, d, e, f, g)
}
myNumbers([7, 2, 19, 4000, 12, 45, -17]); // 7 2 19 4000 12 45 -17
上面的代码没有问题,但是我们必须为数组的每个元素赋值一个变量,有很多重复代码。你可以使用循环来减少一些代码:
const myNumbers = (arrOfNumbers) => {
arrOfNumbers.forEach((value) => {
console.log(value);
})
}
myNumbers([7, 2, 19, 4000, 12, 45, -17]); // 7, 2, 19, 4000, 12, 45, -17
循环没有问题但是我们却向代码中添加了逻辑。
解构使代码简洁:
const myNumbers = (arrOfNumbers) => {
const [a, b, c, d, e, f, g] = arrOfNumbers;
console.log(a, b, c, d, e, f, g); // 7 2 19 4000 12 45 -17
}
myNumbers([7, 2, 19, 4000, 12, 45, -17]);
就是这么简单,解构使左侧的表达式与右侧对应并按顺序赋值。
使用默认值
左右两侧并不总是长度对等,在这种情况下我们可以赋予默认值:
let a, b;
[a=19, b=-17] = [1];
console.log(a); // 1
console.log(b); // -17
为 a 赋值 1,由于数组只有一个值,所以 b 只能取默认值 -17。当右侧有额外的值时,将会被忽略。
let a, b;
[a = 19, b = -17] = [1, 2, 3,];
console.log(a); // 1
console.log(b); // 2
不使用临时变量来交换变量
let a = 5;
let b = 15;
[a, b] = [b, a];
console.log(a); // 15
console.log(b); // 5
函数返回值使用解构
function foo() {
return [1, 2];
}
let a, b;
[a, b] = foo();
console.log(a); // 1
console.log(b); // 2
忽略特定的值
有时候你想获取的值并不是连续的,我们可以跳过中间的值:
function foo() {
return [1, 2, 3, 4];
}
let a, b;
[a, , , b] = foo();
console.log(a); // 1
console.log(b); // 4
字符串使用解构
当我们把解构和字符串一起使用的时候,split 方法很方便。
const [firstName, lastName] = "Parwinder Bhagat".split(' ');
console.log(firstName); // Parwinder
console.log(lastName); // Bhagat
为对象属性赋值
let user = {};
[user.firstName, user.lastName] = ["Parwinder", "Bhagat"];
console.log(user); // { firstName: 'Parwinder', lastName: 'Bhagat' }
解构与扩展运算符
如果你想前面几个值单独赋值,后面的想归为一起,可以使用解构和扩展运算符一起使用来达到效果:
let [name1, name2, ...remaining] = ["Parwinder", "Lauren", "George", "Eliu", "Gaurav"];
console.log(name1); // Parwinder
console.log(name2); // Lauren
console.log(remaining.length); // 3
console.log(remaining[0]); // George
console.log(remaining); // [ 'George', 'Eliu', 'Gaurav' ]
猜你喜欢
- 2024-09-30 JavaScript数组_数组方法「二」(二十七)
- 2024-09-30 table组件,前端如何使用table组件打印数组数据
- 2024-09-30 前端数组改字符串方法 前端数组改字符串方法是什么
- 2024-09-30 javascript复制数组的三种方式 javascript复制粘贴
- 2024-09-30 第21节 检测数组、类数组及多维数组-Web前端开发之Javascript
- 2024-09-30 前端系列——ES6中循环数组的方法
- 2024-09-30 前端已死?请用TS写出20个数组方法的声明
- 2024-09-30 springboot项目中,前端如何传递一个自定义对象数组给后端
- 2024-09-30 带你走进javascript数组的世界 javascript数组操作方法
- 2024-09-30 每天学点 ES6 —— 数组(二) es6数组处理方法
你 发表评论:
欢迎- 05-10如何优化数据库和前端之间的交互?
- 05-10前端代码优化小秘籍(前端优化24条建议)
- 05-10VS Code当中的15个神仙插件,值得收藏
- 05-10如何自己开发一个Google浏览器插件?
- 05-10前端流行框架Vue3教程:14. 组件传递Props效验
- 05-10吃了一年的SU,最好用的插件都在这了
- 05-10前端必看!这款神器让网站界面告别千篇一律
- 05-10程序员请收好:10个非常有用的 Visual Studio Code 插件
- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端md5加密 (49)
- 前端路由 (55)
- 前端数组 (65)
- 前端定时器 (47)
- 前端懒加载 (45)
- 前端接口 (46)
- Oracle RAC (73)
- oracle恢复 (76)
- oracle 删除表 (48)
- oracle 用户名 (74)
- oracle 工具 (55)
- oracle 内存 (50)
- oracle 导出表 (57)
- oracle查询数据库 (45)
- oracle约束 (46)
- oracle 中文 (51)
- oracle链接 (47)
- oracle的函数 (57)
- mac oracle (47)
- 前端调试 (52)
- 前端登录页面 (48)
本文暂时没有评论,来添加一个吧(●'◡'●)