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

165
Vistas
How to rearrange array of objects to new array of Objects

I have an array of objects with the same keys, example:

const data = [{name: "firstName", value: "John"},
{name: "lastName", value: "Smith"},
{name: "Number", value: "421-8686"}
]

I however want the values to become the key and values. example:

const data = [{"firstName": "John"},
{"lastName": "Smith"},
{"Number": "421-8686"}]

I tried a few methods, but for some reason or another I fail. My latest attempt is:

   let newData = {}
    arrayData.forEach((item, index) => {
      let key = data.name
      let value = data.value
      newData[index] = { key: value} // this results in {"key": "John"} --> but i want {"firstName": "John"}

});

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

const data = [{name: "firstName", value: "John"},
{name: "lastName", value: "Smith"},
{name: "Number", value: "421-8686"}
]


/*const data = [{"firstName": "John"},
{"lastName": "Smith"},
{"Number": "421-8686"}]
*/
for(let i = 0;i < data.length;i++){
   
    data[i][data[i].name] = data[i].value  // creates new key value pair
    delete data[i].name                    // deletes old key
    delete data[i].value
    }
    

console.log(data)

about 4 years ago · Santiago Trujillo Denunciar

0

You can use Object.fromEntries.

let data = [
  {name: "firstName", value: "John"},
  {name: "lastName", value: "Smith"},
  {name: "Number", value: "421-8686"}
]

data = data.map(info => Object.fromEntries([[info.name, info.value]]))

console.log(data)

about 4 years ago · Santiago Trujillo Denunciar

0

I believe you should get result as following, what you want. Of course, I confirmed status.

const data = [{name: "firstName", value: "John"},
{name: "lastName", value: "Smith"},
{name: "Number", value: "421-8686"}
]

let newData = [];
data.forEach((item) => {
  let obj = {};
  obj[item.name] = item.value;
  newData.push( obj );
});
    
console.log(newData);

Thanks.

about 4 years ago · Santiago Trujillo 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