klinore
klinore

Reputation: 2739

Destructure an object parameter two levels?

So, I'm passing an object to an ES6 function that I'd like to destructure down to the parameter of a parameter. For example, the code below will log the data prop of stuff, but I'd like it to log the things prop of data of stuff. So the correct answer would log [1,2,3,4]. Not confusing at all, I know. Anyone know if this is possible?

const stuff = {
   data: {
      things: [1,2,3,4]
   }
};
const getThings = ({ data }) => {
   console.log(data)    
};
getThings(stuff);

Upvotes: 37

Views: 16809

Answers (2)

Amit
Amit

Reputation: 46323

Sure, here's how:

const stuff = {
   data: {
      things: [1,2,3,4]
   }
};
const getThings = ({ data: {things} }) => {
   console.log(things)    
};
getThings(stuff);

Upvotes: 74

1ven
1ven

Reputation: 7026

As I correctly understood you, the correct answer is:

const getThings = ({ data: { things } }) => {
   console.log(things)    
};

Upvotes: 11

Related Questions