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
How to move a key value to an new array inside an object

From below object

{
    "user": {
        "id": 9,
        "email": "abcd@example.com",
    },
    "country": "USA",
    "first_name": "firstName",
    "last_name": "lastName",
}

How can we move first_name and last_name to a new array named names and push that into the same object as below

{
    "user": {
        "id": 9,
        "email": "abcd@example.com",
    },
    "country": "USA",
    "name": [
        {
            "first_name": "firstName",
            "last_name": "lastName",
        }
    ],
}

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Destructure the first_name and last_name from the rest of the properties, then create a new object; spreading out the old saved properties, and adding an array with an object with first_name and last_name properties.

const obj={user:{id:9,email:"abcd@example.com"},country:"USA",first_name:"firstName",last_name:"lastName"};

const { first_name, last_name, ...rest } = obj;

const newObj = {
  ...rest,
  names: [ { first_name, last_name } ]
};

console.log(newObj);

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