【问题标题】:Dynamically Creating object keys动态创建对象键
【发布时间】:2020-10-11 17:30:24
【问题描述】:

我一直在尝试理解和掌握使用 javascript 创建 json 对象,但是我似乎无法弄清楚如何(首先迭代某些内容,然后使用该值作为我的键)。我的第二个问题是当我看到我的 json 结果时,它似乎总是与前面的空白项嵌套在一起。这就是我的意思:

我当前的代码部分是:

.then((data)=>{
    connection.query("SELECT * FROM amion_onCall", function (err, result, fields){
        const fixed = [];
            let i;
            for (i in result){
                aName = result[i].name;
                aServ = result[i].specialty;
                aShift = result[i].shift;
                aOff = result[i].office;
                aCell = result[i].cell;
                aTag  = result[i].tag;
                var data = {aServ: {name:aName, service: aServ, shift: aShift, office: aOff, cell: aCell, tag: aTag}};
                // console.log(data);
                fixed.push(data);
            }
        fs.writeFile('./data/json/newAmion.json', JSON.stringify(fixed), function(err){
            if (err) throw err;
            console.log("Wrote New Amion");
        });
    });
})

我的 json 查看器中的输出是:

[
 {
   "aServ": {
      "name": "Dr.John",
      "service": "Cardiology",
      "shift": "7a-7p",
      "office": "123",
      "cell": "123-456-789",
      "tag": "no tags"
  }
}, 

...对于我的大约 150 个条目,依此类推。

问题#1:我想将它提升一整级。我不知道该怎么做,也不知道为什么它开始嵌套如此之深。

问题 #2: 当我迭代 aServ 时,我希望在我的 json 开头输出实际值。我当前的代码为每个人静态打印“aServ”……我不想那样做。例如,这就是我试图让我的 json 输出的方式:

{
  "Cardiology": {
     "name": "Dr.John",
      "service": "Cardiology",
      "shift": "7a-7p",
      "office": "123",
      "cell": "123-456-789",
      "tag": "no tags"
    }, 
  "Pulmonology": { ...and so on
}

【问题讨论】:

    标签: javascript json properties iteration key


    【解决方案1】:

    答案 1:

    “前面的空白项”是什么意思?你的意思是“服务”吗?如果是,因为你在这里做了 var data = {aServ: { ... } }

    另外,你有很多东西。所以这些都在数组[]内。

    答案 2:

    // Change it to object instead of array
    const fixed = {};
    
    // Inside array
    if (!Array.isArray(fixed[aServ])) {
      fixed[aServ] = []  
    }
    
    fixed[aServ].push({
      name: aName,
      service: aServ,
      shift: aShift,
      office: aOff,
      cell: aCell,
      tag: aTag,
    })
    

    无需将项目推送到数组。

    请记住,如果有多个 aServ 具有相同的值,那么它将替换现有项目。这就是你应该使用数组的原因。

    【讨论】:

    • 我的意思是,当我在 json 程序中打开它时,它看起来像这样:Root > Item[0] > aServ > name, service, shift, office, cell tag....而不是我想删除项目[0],并将其带到:Root> Cardiology> name, service, shift.etc。如果我要像这样创建它: var data = {name: aName, service: aService...etc} 我仍然会在我的 json 之前得到一个前面的空白,例如: (item[0]: name, service, cell.. etc},我不想拥有 item[0]
    • 更新了答案。
    • 知道了!谢谢!!将其更改为对象时,我似乎遇到了一个新错误:推送功能似乎不再起作用。我如何将所有迭代放入对象中?
    • 最后一个问题。使用数组而不是对象是有意义的。但是没有实际的方法可以让我可以命名数组项?我当前的代码输出:item[0]、item[1]、item[2]...等,但我希望它将“心脏病学、肺病学等”列为包含嵌套信息的数组。
    • 更新代码。像这样的东西应该工作。没有测试代码。但是你应该明白我想要做什么。我将不胜感激。
    【解决方案2】:

    问题#1:我想将它提升一整级。我不知道该怎么做,也不知道为什么它开始嵌套如此之深。

    它是嵌套的,因为您位于对象数组中。 JavaScript 中的数组由括号 [] 表示。在这种情况下,我认为这就是您想要的:您正在创建一个对象数组。

    问题 #2: 当我迭代 aServ 时,我希望在我的 json 开头输出实际值。

    这一行:

     var data = {aServ: {name:aName, service: aServ, shift: aShift, office: aOff, cell: aCell, tag: aTag}}
    

    需要改成这样:

     var data = {[aServ]: {name:aName, service: aServ, shift: aShift, office: aOff, cell: aCell, tag: aTag}}
    

    注意括号。这会将您的变量值插入为数组键。

    【讨论】:

      猜你喜欢
      • 2020-03-01
      • 2019-10-20
      • 2015-09-08
      • 2013-11-19
      • 2019-07-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多