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

228
Vistas
Convert JSON Object values to keys in a json array

Input

data = 
[
 { "name": "AAA", "uuid": "111", "zone": "A"},
 { "name": "BBB", "uuid": "222", "zone": "B"},
 { "name": "CCC", "uuid": "333", "zone": "C"},
]

Desired Output

data = 
[
    { 
       "AAA": {"uuid": "111", "zone": "A"},
       "BBB": {"uuid": "222", "zone": "B"},     
       "CCC": {"uuid": "333", "zone": "C"}, 
    }           
]

I tried this,

data = 
    [
     { "name": "AAA", "uuid": "111", "zone": "A"},
     { "name": "BBB", "uuid": "222", "zone": "B"},
     { "name": "CCC", "uuid": "333", "zone": "C"},
    ];
data = data.map( o => new Object({"uuid": o.uuid, "zone": o.zone}));
console.log(data);

it gives

[
{
  uuid: "111",
  zone: "A"
}, {
  uuid: "222",
  zone: "B"
}, {
  uuid: "333",
  zone: "C"
}
]

I want the "name" field to be the key for each object.

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

0

The parts of the output that correspond to the input are all inside a single item in an array, so map inside an array literal. Extract the name property when mapping, and return an object whose key is that property, and the value is the rest of what's in the object (with rest syntax).

const input = [
     { "name": "AAA", "uuid": "111", "zone": "A"},
     { "name": "BBB", "uuid": "222", "zone": "B"},
     { "name": "CCC", "uuid": "333", "zone": "C"},
    ];
const output = [input.map(({ name, ...rest }) => ({ [name]: rest }))];
console.log(output);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Array#reduce is another way to iterate your array. You can create an object with the names as keys

let data = [{ "name": "AAA", "uuid": "111", "zone": "A"},
     { "name": "BBB", "uuid": "222", "zone": "B"},
     { "name": "CCC", "uuid": "333", "zone": "C"}];
     
data = data.reduce((b,a) => ({...b,  [a.name]: {"uuid": a.uuid, "zone": a.zone}}), {});
console.log(data);

Output:

{
  "AAA": {
    "uuid": "111",
    "zone": "A"
  },
  "BBB": {
    "uuid": "222",
    "zone": "B"
  },
  "CCC": {
    "uuid": "333",
    "zone": "C"
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use object destructuring syntax acomplish this easily.

Using your original data :

let data = [
  { "name": "AAA", "uuid": "111", "zone": "A" },
  { "name": "BBB", "uuid": "222", "zone": "B" },
  { "name": "CCC", "uuid": "333", "zone": "C" }
];

Create a new Object to hold our restructured data:

let records = {};

Here we destructure each data object by assigning the name property to it's own variable and the remaining properties to the new Object r with the spread operator (...). Then we can use bracket notation to assign the data to the records object by name:

data.forEach(({ name, ...r }) => records[name] = r);

If we check the records object :

console.log(records);

{
  AAA: {
    uuid: "111",
    zone: "A"
  },
  BBB: {
    uuid: "222",
    zone: "B"
  },
  CCC: {
    uuid: "333",
    zone: "C"
  }
}

records is not an Array and cannot be accessed by index.

console.log(records[0]); // undefined

But can now be referenced by key:

console.log(records['AAA']); // { uuid: "111", zone: "A" }
console.log(records.BBB); // { uuid: "222", zone: "B" }

If you absolutely want your data as an Object nested inside an Array:

data = [records];
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