Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
how to get nested object value inside a function in javascript while object destructuring?

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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to get a specific attribute inside details, then you can directly access it:

function output({value, details}){
    console.log(`value: ${value} , detail: ${details.serialNumber}`);
}

or equivalently, as suggested by Nick Parsons in the comments:

function output({value, details: {serialNumber}}){
    console.log(`value: ${value} , detail: ${serialNumber}`);
}

If instead you want to show the full object contained in details, then you can use JSON.stringify, as explained here. Have a look at this piece of code.

const secondObject = {
    value : 2,
    credit: 23, 
    details: {
        serialNumber : 4,
        fooBar: 42
    }
}

function output({value, details}){
    str = JSON.stringify(details);
    console.log(`value: ${value} , detail: ${str}`);
}

output(secondObject);
// value: 2 , detail: {"serialNumber":4,"fooBar":42}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda