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

73
Vistas
How to move object properties inside an array of objects

I have an array of objects like this:

currentArray = [
  {
    year: 2011,
    asset: {
      silver: 322,
      gold: 325,
    },
  },
  {
    year: 2012,
    asset: {
      silver: 411,
      gold: 2235,
    },
  },
];

What needs to be done in JavaScript/TypeScript, to change it from the current structure to the structure below? The silver- and gold-property of each asset-object should be stored in the object above, next to 'year', and the asset-objects need to be deleted:

desiredArray = [
  {
    year: 2011,
    silver: 322,
    gold: 325,
  },
  {
    year: 2012,
    silver: 411,
    gold: 2235,
  },
];
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

let currentArray = [{
    year: 2011,
    asset: {
      silver: 322,
      gold: 325,
    },
  },
  {
    year: 2012,
    asset: {
      silver: 411,
      gold: 2235,
    },
  },
];

currentArray.map(item => {
  Object.assign(item, item.asset)
  delete item.asset;
  
  return item
})

console.log(currentArray)

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