Reputation: 2739
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
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
Reputation: 7026
As I correctly understood you, the correct answer is:
const getThings = ({ data: { things } }) => {
console.log(things)
};
Upvotes: 11