while practicing object destructuring ; I faced some problems.
I have a object like this:
const secondObject = {
value : 2,
credit: 23,
details: {
serialNumber : 4
}
}
Now I want to get 'serialNumber' value inside a function parameter. so I invoke a function like this :
function output({value, details}){
console.log(`value: ${value} , detail: ${details}`);
}
output(secondObject);
when I log it into console the result showing : detail: [object Object]
*but if I do like this ,I got the serialNumber's value : *
const {details: {serialNumber}} = secondObject;
console.log(serialNumber);
can anyone tell me how can I get the value inside a function?
If you want to get a specific attribute inside
details
, then you can directly access it:or equivalently, as suggested by Nick Parsons in the comments:
If instead you want to show the full object contained in
details
, then you can useJSON.stringify
, as explained here. Have a look at this piece of code.