UserBSS1
UserBSS1

Reputation: 2211

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.

Upvotes: 0

Views: 1155

Answers (3)

Besworks
Besworks

Reputation: 4483

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

Upvotes: 1

Kinglish
Kinglish

Reputation: 23654

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

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370729

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

Upvotes: 2

Related Questions