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

150
Visualizações
Destructuring an object in order to make the all properties none nested / single level

I have this object with the following nested properties.

{
  _id: {
    agent_info: {
      name: 'Ritu',
      status: 'active',
      avatar: null
    }
  },
  avg_score: 100,
}

I am trying to deconstruct it so that it looks like this.

    {
          name: 'Ritu',
          status: 'active',
          avatar: null,
          avg_score: 100,
    }

I tried using this const { _id:{...agent_info} } = user; but the output remains the same and the object is not altered.

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

0

Destructuring won't alter the object. It might create a new object, but it won't modify the original.

You can't use a single destructuring operation to get the result you want. You can get close, but it takes two steps.

const { _id: { agent_info: { ...result } } } = original;
result.avg_score = original.avg_score;

Live Example:

const original = {
  _id: {
    agent_info: {
      name: 'Ritu',
      status: 'active',
      avatar: null
    }
  },
  avg_score: 100,
};

const { _id: { agent_info: { ...result } } } = original;
result.avg_score = original.avg_score;
console.log(result);

That copies everything from original._id.agent_info into a new object referenced by the result constant, then adds in avg_score.

You could also do it without reusing original, by grabbing avg_score in the same operation that creates result:

const { _id: { agent_info: { ...result } }, avg_score } = original;
result.avg_score = avg_score;

Live Example:

const original = {
  _id: {
    agent_info: {
      name: 'Ritu',
      status: 'active',
      avatar: null
    }
  },
  avg_score: 100,
};

const { _id: { agent_info: { ...result } }, avg_score } = original;
result.avg_score = avg_score;
console.log(result);

...but that leaves an avg_score constant lying around (which is harmless, but...).

about 4 years ago · Juan Pablo Isaza Relatório

0

You can achieve this object destructuring using something that is called deep property. enter image description here

const obj = {
  _id: {
    agent_info: {
      name: 'Ritu',
      status: 'active',
      avatar: null
    }
  },
  avg_score: 100,
};

const {_id:{ agent_info:{ ...res }}} = obj;
res["avg_Score"] = obj.avg_score;

console.log(res);

Check out the this link for more info

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