javascript - 在最新的 JavaScript 中,这个更短的符号是什么?
问题描述
我有这个:
const id = speakerRec.id;
const firstName = speakerRec.firstName;
const lastName = speakerRec.lastName;
我认为有类似的东西,但不记得了。
const [id, firstName, lastName] = speakerRec;
解决方案
您需要{}
用于解构对象属性:
const {id, firstName, lastName} = speakerRec;
[]
用于数组解构:
const [one, two, three] = [1, 2, 3];
示范:
const speakerRec = {
id: "mySpeaker",
firstName: "Jack",
lastName: "Bashford"
};
const { id, firstName, lastName } = speakerRec;
console.log(id);
console.log(firstName);
console.log(lastName);
const [one, two, three] = [1, 2, 3];
console.log(one);
console.log(two);
console.log(three);
推荐阅读
- typescript - 在 Typescript 中访问动态/泛型类的属性
- c# - 从类型字典创建动态实例
- javascript - 如何使用设备浏览器而不是 App 的 webview?
- snakemake - Snakemake 显式处理内存不足 (OOM) 故障
- javascript - Array.find() 给出错误“TypeError: theArray.find(...) is undefined”
- python - Python3,imaplib,expunge 命令在文件夹中的所有消息的循环中用作每个消息
- javascript - Javascript - 为什么在迭代函数中测量时间变得不准确?
- user-interface - 为什么我在使用 useSelector 钩子时将键名作为我的减速器名称
- c++ - CUDA C++ - 如何在 cuda 内核中定义一个未知大小的数组(没有 __shared__ )?
- mysql - Power Automate - 与 MySQL 的云连接