Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!