How to get a data from an object inside of a function (JS)

209 views Asked by At

let's say that I have a function that includes an object

const someFunc =props=> ({
styles:{
somedata: 'somedata:',
},
infoSectionValue: {
 bold: true,
},
images:{
 donut: props.donut_base64,
icon: 'some icon'
},
 content: [1,2,3]
}
})`
now I want to get the icon using object destruction
`const {images :{ icon }} = someFunc()
console.log(icon)

ERROR : props not define it didn't work while there are other objects or props included how can I get the icon in this case?

this is only way only have worked, if the function is to maintain the object:

const someFunc =props=> {
images:{
icon: 'some icon'
}
}
1

There are 1 answers

2
Tommy Hansen On

You probably mean object destructuring, not destruction.

This works (you're missing parenthesis):

Edit: There are multiple errors in your example code, this shows a couple of options:

 const someFunc = props => ({
    foo: 87,
    bar: { foo: 'hi' }

});

const { bar: { foo: msg } } = someFunc();

console.log(msg);

const { bar: bar } = someFunc();

console.log(bar)