JSON对象属性转化为JSON对象名称

JSON Object property into JSON Object name

我有一个 JSON 文件,其中包含很多这样的对象(例如 Pikemen 单位的属性 >Heroes 3 btw<):

[ {
        "ID": 0,
        "AI": 80,
        "": null,
        "Level": 0,
        "Growth": 14,
        "Low": 20,
        "High": 50,
        "Town": 0,
        "Name": "Pikeman",
    }, etc.. ]

如何让 "Name" 属性 成为 JSON 文件中每个对象的名称?在 JS 代码中或通过格式化文件。我的意思是:

[ "Pikeman": {
       "ID": 0,
       "AI": 80,
       etc..
} ]

这样使用:

let obj = [{
        "ID": 0,
        "AI": 80,
        "": null,
        "Level": 0,
        "Growth": 14,
        "Low": 20,
        "High": 50,
        "Town": 0,
        "Name": "Pikeman",
    },{
        "ID": 0,
        "AI": 82220,
        "": null,
        "Level": 230,
        "Growth": 14,
        "Low": 20213,
        "High": 12312050,
        "Town": 10,
        "Name": "Pikeman2",
    }]

obj.forEach((item, index) => {
  obj[item.Name] = item;
  delete obj[index];
})

console.log(obj)

你好@Adrian 你可以遍历你的数组并获取名称并将 int 指定为键检查这个:

let obj = [ {
        "ID": 0,
        "AI": 80,
        "": null,
        "Level": 0,
        "Growth": 14,
        "Low": 20,
        "High": 50,
        "Town": 0,
        "Name": "Pikeman",
    }, 
    {
        "ID": 0,
        "AI": 80,
        "": null,
        "Level": 0,
        "Growth": 14,
        "Low": 20,
        "High": 50,
        "Town": 0,
        "Name": "Pikeman1",
    }, 
    {
        "ID": 0,
        "AI": 80,
        "": null,
        "Level": 0,
        "Growth": 14,
        "Low": 20,
        "High": 50,
        "Town": 0,
        "Name": "Pikema2",
    }]

    let objConverted = {};

    obj.forEach( o => {
      objConverted[o.Name] = o; 
    } ) 

    console.log(objConverted);

如果您想使用代码,请选中此选项:https://repl.it/repls/HalfSatisfiedMainframe