javascript - using for (key in json) - I would like to get every other key note json consists only of objects

北慕城南 提交于 2020-12-15 05:40:08

问题


I would like to use for (key in json) but only use every other key. It would be simple using arrays, but the json I recieve consists only of objects.

I have already asked a smililair question and th nicee people pointed out a typo and why it didn't work. The problem (I am still having) is that the content is duplicated (ie: I would like the 1st object in id="1" and the 2cnd object in id="2", then the 3rd object again in id="1" and the 4th object again in id="2" etc.)

The problem is that I get the 1st object in id="1" and id="2", then the 2cnd object again in id="1" and id="2".

For example lets say I have this json file:

"123":{
   "name":"someName",
   "age":"12",
   "health":{
      "heart":"OK",
      "lungs":"Not so good"
    }
},
"223":{
   "name":"someName1",
   "age":"42",
   "health":{
      "heart":"Not so good",
      "lungs":"OK"
    }
}

I would like to get name and lungs of the 1st patient into div id="1" and the 2cnd persons name and lungs into div id="2".

What I would like:
id="1" ... SomeName, OK
id="1" ... SomeName1, Not so good

What I actually get is:
id="1" ... SomeName, OK, SomeName1, Not so good
id="2" ... SomeName, OK, SomeName1, Not so good

so the question is how do I get every other key from the for (key in json) function.

Here is my code. Sorry for the long text, just wanted to make things clear. If you have any additional questions, let me know. Thanks.

    var data1 = document.getElementById("data1");
    var data2 = document.getElementById("data2");
    loadJSON(function(json) {
      var l = Object.keys(json).length;
      console.log("json start");
      for (var i = 0; i <= l; i++){
        console.log(i);
        if (i % 2 === 0){
          for (x in json) {
            data1.innerHTML+="<img src=" + json[x].picture + "/>";
            data1.innerHTML+=json[x].price_wrapper.price + json[x].price_thingy.suf + " modulo 0 + " + i;
            console.log("0" + i);
          }
        } else {
          for (x in json) {
            data2.innerHTML+="<img src=" + json[x].picture + "/>";
            data2.innerHTML+=json[x].price_wrapper.price + json[x].price_thingy.suf + " modulo 1 + " + i;
            console.log("1" + i);
          }
        } 
      }
    });

回答1:


Solved.

The credit goes to @Ashwin.

He reworked the code a little bit and now it works.

loadJSON(function(json) {
  console.log("json start");
  var i = 0;
  var l = Object.keys(json).length;
  for (x in json){
    console.log(i);
    if (i % 2 === 0){
      data1.innerHTML+="<img src=" + json[x].picture + "/>";
      data1.innerHTML+=json[x].price_thingy.price + json[x].price_thingy.suf;
      console.log("0 " + l);
    } else {
      data2.innerHTML+="<img src=" + json[x].picture + "/>";
      data2.innerHTML+=json[x].price_thingy.price + json[x].price_thingy.suf;
      console.log("1 " + l);
    }
    i++;
  }
});



回答2:


You are looping throught the entire json again. If I understand right you want only values of the json object with key l[i].

let json = 
  {
    "123":{
      "name":"someName",
      "age":"12",
      "health":{
        "heart":"OK",
        "lungs":"Not so good"
      }
    },
    "223": {
      "name":"someName1",
      "age":"42",
      "health":{
        "heart":"Not so good",
        "lungs":"OK"
      }
    }
  }

let l = Object.keys(json)
console.log(l.length)

for(let i = 0; i < l.length; i++){
  let element = json[l[i]];
  console.log(`id=${i}, ${element.name}, ${element.health.lungs}`);
}

Here is my example :

https://jsbin.com/yavosiroxi/edit?js,console



来源:https://stackoverflow.com/questions/64355111/javascript-using-for-key-in-json-i-would-like-to-get-every-other-key-note

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!