Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda