简体   繁体   English

动态创建 object 密钥

[英]Dynamically Creating object keys

I've been trying to understand and master creating json objects with javascript, however I cannot seem to figure out how to (first iterate something, and use that value as my key).我一直在尝试理解和掌握使用 javascript 创建 json 对象,但是我似乎无法弄清楚如何(首先迭代一些东西,并将该值用作我的键)。 My second problem is when I see my json result, it always seems to be nested with a preceding blank item before it.我的第二个问题是当我看到我的 json 结果时,它似乎总是与前面的空白项嵌套在一起。 Here is what I mean:这就是我的意思:

My current portion of code is:我当前的代码部分是:

.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");
        });
    });
})

The output in my json viewer is:我的 json 查看器中的 output 是:

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

...and so on for my ~150 entries. ...等等我的〜150个条目。

Problem #1: I want to move this up one full level.问题#1:我想将它提升一个完整的级别。 I'm not sure how to do that, or why it starts so deeply nested.我不确定如何做到这一点,或者为什么它开始如此深入地嵌套。

Problem #2: When I iterate aServ, I want the actual value to be output in the beginning of my json.问题 #2:当我迭代 aServ 时,我希望实际值是 json 开头的 output。 My current code prints "aServ" statically for everyone...I don't want to do that.我当前的代码为每个人静态打印“aServ”……我不想那样做。 For example, this is how I am trying to get my json to output:例如,这就是我试图让我的 json 到 output 的方式:

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

Answer 1:答案1:

What do you mean by "preceding blank item"? “前面的空白项”是什么意思? Do you mean "aServe"?你的意思是“服务”吗? If yes, because you did it here var data = {aServ: {... } } .如果是,因为你在这里做了var data = {aServ: {... } }

Also, you have a lot of items.另外,你有很多东西。 So those are inside array [] .所以那些在数组[]里面。

Answer 2:答案 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,
})

No need to push item to array.无需将项目推送到数组。

Remember, if there are multiple aServ with the same value then it will replace the existing item.请记住,如果有多个aServ具有相同的值,那么它将替换现有项目。 That is why you should use an array.这就是为什么你应该使用数组。

Problem #1: I want to move this up one full level.问题#1:我想将它提升一个完整的级别。 I'm not sure how to do that, or why it starts so deeply nested.我不确定如何做到这一点,或者为什么它开始如此深入地嵌套。

It's nested because you're in an array of objects.它是嵌套的,因为您位于对象数组中。 Arrays in JavaScript are indicated by the surrounding brackets []. JavaScript 中的 Arrays 用括号 [] 表示。 In this case I think it's what you want: you're creating an array of objects.在这种情况下,我认为这就是您想要的:您正在创建一个对象数组。

Problem #2: When I iterate aServ, I want the actual value to be output in the beginning of my json.问题 #2:当我迭代 aServ 时,我希望实际值是 json 开头的 output。

This line:这一行:

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

Needs to be changed to this:需要改成这样:

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

Note the brackets.注意括号。 That will interpolate your variable values as an array key.这会将您的变量值插入为数组键。

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

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