如何在 ES6 中销毁嵌套在一个对象中的两个变量
How to destructing two variables that are nested in an object in ES6
假设我有这样的对象
const user = {
id: 339,
name: 'Fred',
age: 42,
education: {
getDegree: () => {} //function
}
};
const {education: {getDegree}} = user;
我经常有一个用例需要同时获得教育和学位作为用户对象的参数。
我只知道如何从obj中析构getDegree,如何获取education变量呢?
做同样的事情,但我相信有更好的方法吗?
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);
假设我有这样的对象
const user = {
id: 339,
name: 'Fred',
age: 42,
education: {
getDegree: () => {} //function
}
};
const {education: {getDegree}} = user;
我经常有一个用例需要同时获得教育和学位作为用户对象的参数。
我只知道如何从obj中析构getDegree,如何获取education变量呢?
做同样的事情,但我相信有更好的方法吗?
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);