javascript - 如何销毁嵌套在 ES6 对象中的两个变量
问题描述
假设我有 obj 喜欢
const user = {
id: 339,
name: 'Fred',
age: 42,
education: {
getDegree: () => {} //function
}
};
const {education: {getDegree}} = user;
我经常有一个用例需要从用户 obj 获取教育和学位作为参数。
我只知道如何从obj中破坏getDegree,如何获取教育变量呢?
做同样的事情,但我相信有更好的方法来做到这一点?
const {education: {getDegree}} = user;
const {education} = user;
解决方案
只需education
在解构中列出:
const user = {
id: 339,
name: 'Fred',
age: 42,
education: {
foo: "bar"
}
};
const {education, education: {foo}} = user;
console.log(education);
console.log(foo);
推荐阅读
- unity3d - 抖动字符控制器
- javascript - 如何解决错误:错误:在 PoolConnection.Connection._handleConnectTimeout 处连接 ETIMEDOUT?
- php - 从网站联系表接收电子邮件但没有内容
- c++ - 从向量数组中擦除特定值
- java - 如何从java中的资源文件夹中读取文件?
- shopify - 在没有锚标签的情况下拉出帐户页面网址的简码是什么?
- sql - 将列中具有相同值的连续行减少为单行
- jquery - 在提交或阻止提交之前验证表单(不显眼)时如何使用 jquery .defer 或 .done
- c# - Unity 触摸/鼠标碰撞而不是 MouseDown()
- sql - 如何查询在postgres中以字符串形式存在的数组?