1

Let's say I have obj like

const user = {
 id: 339,
 name: 'Fred',
 age: 42,
 education: {
   getDegree: () => {} //function
 }
};
const {education: {getDegree}} = user;

I often have a use case that needs to get both education and degree as parameter from user obj.

I only know how to destruct getDegree from obj, what to do to get the education variable as well?

something to do the same thing, but I believe there is a better way to do this?

const {education: {getDegree}} = user;
const {education} = user;
Xinrui Ma
  • 2,065
  • 5
  • 30
  • 52

1 Answers1

4

Just list education in the destructure as well:

const user = {
 id: 339,
 name: 'Fred',
 age: 42,
 education: {
   foo: "bar"
 }
};
const {education, education: {foo}} = user;

console.log(education);
console.log(foo);
zero298
  • 25,467
  • 10
  • 75
  • 100