简体   繁体   English

将 JSON Object 值转换为 json 数组中的键

[英]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所需 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.我希望“名称”字段成为每个 object 的键。

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. output 对应于输入的部分都在数组中的单个项目中,因此 map数组文字中。 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).映射时提取name属性,并返回一个 object,其键是该属性,值为 object 中的 rest(使用 rest 语法)。

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

Array#reduce is another way to iterate your array. Array#reduce是迭代数组的另一种方法。 You can create an object with the names as keys您可以使用名称作为键创建一个 object

 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: Output:

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

You can use object destructuring syntax acomplish this easily.您可以使用object 解构语法轻松完成此操作。

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:创建一个新的 Object 来保存我们重组后的数据:

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 ( ... ).在这里,我们解构每个数据 object,方法是使用扩展运算符( ... ) 将name属性分配给它自己的变量,并将其余属性分配给新的 Object r Then we can use bracket notation to assign the data to the records object by name:然后我们可以使用括号表示法将数据按名称分配给records object:

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

If we check the records object:如果我们查看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. records不是数组,不能通过索引访问。

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

But can now be referenced by key:但是现在可以通过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:如果您绝对希望将数据作为 Object 嵌套在数组中:

data = [records];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM