Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda