by Joanna Gaudyn

乔安娜·高登(Joanna Gaudyn)

Destructuring was a new addition to ES6. It took inspiration from languages like Python and allows you to extract data from arrays and objects into distinct variables. It might sound like something you’ve done in the earlier versions of JavaScript already, right? Have a look at two examples.

销毁是ES6的新增功能。 它从Python之类的语言中汲取了灵感,并允许您从数组和对象中提取数据到不同的变量中。 听起来您已经在早期版本JavaScript中完成过操作,对吧? 看两个例子。

The first one extracts data from an object:

第一个从对象中提取数据:

const meal = {  name: 'pizza',  type: 'marinara',  price: 6.25};
const name = meal.name;const type = meal.type;const price = meal.price;
console.log(name, type, price);

Prints:

印刷品:

pizza marinara 6.25

比萨比萨饼6.25

And the second one from an array:

第二个来自数组:

const iceCreamFlavors = ['hazelnut', 'pistachio', 'tiramisu'];const flavor1 = iceCreamFlavors[0];const flavor2 = iceCreamFlavors[1];const flavor3 = iceCreamFlavors[2];console.log(flavor1, flavor2, flavor3);

Prints:

印刷品:

hazelnut pistachio tiramisu

榛子开心果提拉米苏

The thing is, though, that neither of these examples actually uses destructuring.

事实是,这些示例均未真正使用解构。

什么是解构? (What is destructuring?)

Destructuring lets you specify the elements you want to extract from an array or object on the left side of an assignment. This means much less code and exactly the same result as above, without losing readability. Even if it sounds odd at first.

通过解构,您可以指定要从分配左侧的数组或对象中提取的元素。 这意味着更少的代码和与上面完全相同的结果,而不会丢失可读性。 即使一开始听起来很奇怪。

Let’s redo our examples.

让我们重做我们的示例。

销毁对象 (Destructuring objects)

Here’s how we destructure values from an object:

这是我们如何从对象中解构值的方法:

const meal = {  name: 'pizza',  type: 'marinara',  price: 6.25};
const {name, type, price} = meal;
console.log(name, type, price);

Prints:

印刷品:

pizza marinara 6.25

比萨比萨饼6.25

The curly braces { } stand for the object which is being destructured and name, type, and price represent the variables to which you want to assign the values. We can skip the property from where to extract the values, as long as the names of our variables correspond with the names of the object’s properties.

大括号{ }代表正在被分解的对象, nametypeprice表示要为其分配值的变量。 只要变量的名称与对象的属性的名称相对应,就可以跳过从中提取值的属性。

Another great feature of object destructuring is that you can choose which values you want to save in variables:

对象解构的另一个重要功能是,您可以选择要保存在变量中的值:

const {type} = meal; will only select the type property from the meal object.

const {type} = meal; 只会从meal对象中选择type属性。

解构数组 (Destructuring arrays)

Here’s how our original example would be handled:

这是我们原始示例的处理方式:

const iceCreamFlavors = ['hazelnut', 'pistachio', 'tiramisu'];
const [flavor1, flavor2, flavor3] = iceCreamFlavors;
console.log(flavor1, flavor2, flavor3);

Prints:

印刷品:

hazelnut pistachio tiramisu

榛子开心果提拉米苏

The brackets [ ] stand for the array which is being destructured and flavor1, flavor2 and flavor3 represent the variables to which you want to assign the values. Using destructuring we can skip the indexes at which the values live in our array. Convenient, isn’t it?

方括号[ ]表示要进行解构的数组, flavor1flavor2flavor3表示要为其分配值的变量。 使用解构,我们可以跳过值存在于数组中的索引。 方便,不是吗?

Similarly as with an object, you can skip some values when destructuring an array:

与对象类似,在分解数组时可以跳过一些值:

const [flavor1, , flavor3] = iceCreamFlavors; will simply ignore flavor2.

const [flavor1, , flavor3] = iceCreamFlavors; 只会忽略flavor2

Long live laziness as a potent motivator for the invention of new shortcuts!

懒惰万岁是发明新捷径的有力动力!

Did you like this article? Maybe you’ll find these interesting too:

你喜欢这篇文章吗? 也许您也会发现这些有趣的东西:

What does yoga have to do with programming?You might be surprised.medium.freecodecamp.orgSpread operator and rest parameter in JavaScript (ES6)Both the spread operator and the rest parameter are written as three consecutive dots (…). Do they have anything else…medium.comAn overview of JavaScript iteratorsThe difference between for, for…in and for…of loopsmedium.freecodecamp.org

瑜伽与编程有什么关系? 您可能会感到惊讶。 medium.freecodecamp.org JavaScript(ES6)中 的扩展运算符和rest参数扩展运算符和rest参数都被写为三个连续的点(…)。 它们还有其他内容吗…… medium.com JavaScript迭代器概述 for,for…in和for…of循环之间的区别medium.freecodecamp.org

翻译自: https://www.freecodecamp.org/news/destructuring-in-javascript-es6-ee963292623a/

如何充分利用JavaScript(ES6)中的解构功能相关推荐

  1. C# 7.0中的解构功能---Deconstruct

    解构元组 C#7.0新增了诸多功能,其中有一项是新元组(ValueTuple),它允许我们可以返回多个值,并且配合解构能更加方便的进行工作,如下面例子 可以看到解构元组可以写出优雅的代码,并且可以使用 ...

  2. 5个技巧让你更好的编写 JavaScript(ES6) 中条件语句

    使用 JavaScript 时,我们经常需要处理很多条件语句,这里分享5个小技巧,可以让你编写更好/更清晰的条件语句. 1.使用 Array.includes 来处理多个条件 我们来看看下面的例子: ...

  3. ES6新语法--解构赋值

    对象解构赋值 ※很重要 /*** 对象解构赋值:获取元素中属性的值,然后赋值给变量*///声明一个对象 let obj = {name:'chen',age:38,gender:'man',score ...

  4. javascript编写_如何在JavaScript中使用解构来编写更简洁,功能更强大的代码

    javascript编写 by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 如何在JavaScript中使用解构来编写更简洁,功能更强大的代码 (How to ...

  5. es6字符串添加html标签,JavaScript_详解JavaScript ES6中的模板字符串,在 ES6 中引入了一种新的字符 - phpStudy...

    详解JavaScript ES6中的模板字符串 在 ES6 中引入了一种新的字符串字面量 - 模板字符串,除了使用反引号 (`) 表示,它们看上去和普通的字符串没有什么区别.在最简单的情况下,他们就是 ...

  6. js模板字符串自定义类名_详解JavaScript ES6中的模板字符串

    这篇文章主要介绍了详解JavaScript ES6中的模板字符串,JS的ES6版本带来诸多简洁化方面的重大改进,需要的朋友可以参考下 在 ES6 中引入了一种新的字符串字面量 - 模板字符串,除了使用 ...

  7. 如何在 JavaScript 中使用对象解构

    对象解构是一个有用的 JavaScript 功能,用于从对象中提取属性并将其绑定到变量. 更好的是,对象解构可以在一个语句中提取多个属性,可以从嵌套对象访问属性,并且可以在属性不存在时设置默认值. 在 ...

  8. Vue学习笔记(三)Vue2三种slot插槽的概念与运用 | ES6 对象的解构赋值 | 基于Vue2使用axios发送请求实现GitHub案例 | 浏览器跨域问题与解决

    文章目录 一.参考资料 二.运行环境 三.Vue2插槽 3.1 默认插槽 3.2 具名插槽 3.3 作用域插槽 ES6解构赋值概念 & 作用域插槽的解构赋值 3.4 动态插槽名 四.GitHu ...

  9. ES6变量的解构赋值--对象篇

    目录 使用方式 普通形式 嵌套结构 使用默认值 注意事项 上一篇我们讲解了数组的解构赋值,解构还可以用于对象.字符串等等,这里我们来讲解对象的解构赋值. ES6变量的解构赋值--数组_zxn20012 ...

最新文章

  1. Unterminated array at character 6 of xxx
  2. Spring Boot中Web应用的统一异常处理
  3. 解决mysqlAccess denied for user'root'@'IP地址'问题
  4. 【深度学习】深入理解卷积神经网络(CNN)
  5. RocketMQ实现原理
  6. python logging模块的作用_【python】【logging】python日志模块logging常用功能
  7. navicat premium 链接postgresql 无法加载表_PostgreSQL 每周新闻 2020311
  8. react-native-baidu-map使用及注意问题
  9. python keras_深度学习:基于Keras的Python实践
  10. 过滤 php 网址,php过滤html中的其他网站链接的方法(域名白名单功能)
  11. 【机器学习】hist参数解读
  12. STM32——项目需求之低功耗的停机模式
  13. pi/4QPSK调制解调原理
  14. 冒险岛2最新出的服务器,冒险岛2 9月21日零点服务器维护公告一览 几点开服
  15. 立创商城PCB库使用说明
  16. SpringBoot中关于RunWith以及SpringBootTest
  17. 2004-10-30 周六
  18. html怎么所有按钮没效果图,点击按钮没反应?所有按钮都没反应
  19. 我的专业偶像作文计算机,我的崇拜的偶像作文(通用5篇)
  20. arm+linux+分辨率无效,Arm NPU的超分辨率!

热门文章

  1. 实现对学生信息的修改操作
  2. java file_Java IO: File
  3. 通风与防排烟工程电子书_菠菜关于防排烟系统使用软接头工程量计算注意及定额选用建议...
  4. iOS 改变字符串中数字的颜色
  5. 如何编辑PDF文件,PDF编辑器如何使用
  6. SpringBoot b2b2c 多用户商城系统(十五)Springboot整合RabbitMQ...
  7. LLVM官方文档翻译---- LLVM原子指令与并发指引
  8. Linux字符设备驱动程序的框架(新写法)
  9. Android 占位符 %1$s %1$d
  10. Python包管理工具Distribute的安装