Home > Software engineering >  Convert JSON Object values to keys in a json array
Convert JSON Object values to keys in a json array

Time:04-06

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.

CodePudding user response:

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);

CodePudding user response:

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"
  }
}

CodePudding user response:

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];
  • Related