Home > Back-end >  how do I call all the keys of a json?
how do I call all the keys of a json?

Time:06-15

I have this code:

<body>
  <script type="application/json" src="Data0012.json"></script>
    <div><select id="selector"></select></div>
        <div ></div>
        <div ></div>
        <div ></div>




    <script>
      function loadJSON(filePath, callback) {
      var xobj = new XMLHttpRequest();
      xobj.overrideMimeType("application/json");
      xobj.open('GET', filePath, true);
      xobj.onreadystatechange = function() {
        if (xobj.readyState == 4 && xobj.status == "200") {
            // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
            callback(xobj.responseText);
        }
    };
    xobj.send(null);
}
loadJSON("Data0012.json", function(text){
    const data = JSON.parse(text);
    console.log(data);
});

// Get object key
//const keys = Object.keys(obj);

// Stored data
//localStorage.setItem('keys', keys);

// Call method to get stored data when in different scripts
const keys = localStorage.getItem('keys');

console.log(keys)

    </script

and I have this json

{
    "-162.65": {
        "Player": "Gdlachance",
        "Hands": "44",
        "(BTN) PFR (2-2,25) (16 )": "13",
        "!!!0 All-In Equity Adjusted BB/100": "-162.65",
        "BH_MTT_3Bet (BB vs BU open)": "50"
    },
    "-162.27": {
        "Player": "paramasivum",
        "Hands": "40",
        "(BTN) PFR (2-2,25) (16 )": "9",
        "!!!0 All-In Equity Adjusted BB/100": "-162.27",
        "BH_MTT_3Bet (BB vs BU open)": "67"
    },
    "-157.32": {
        "Player": "Fairline69",
        "Hands": "49",
        "(BTN) PFR (2-2,25) (16 )": "25",
        "!!!0 All-In Equity Adjusted BB/100": "-157.32",
        "BH_MTT_3Bet (BB vs BU open)": "17"
    },
    "-51.20": {
        "Player": "Matthinio-10",
        "Hands": "34",
        "(BTN) PFR (2-2,25) (16 )": "40",
        "!!!0 All-In Equity Adjusted BB/100": "-51.20",
        "BH_MTT_3Bet (BB vs BU open)": "80"
    }}

that code gives me the first column ("-162.65", "-162,67","157.32" ......) and the problem is that I whant the second column of json

"Player", "Hands", "(BTN) PFR (2-2,25) (16 )", "!!!0 All-In Equity Adjusted BB/100" "BH_MTT_3Bet (BB vs BU open)":

how can I call all the keys of json? I cant modify the json (I cant add code or whatever) and i dont know what are the values inside the columns (in a csv it would be the first line (cant call them explicitly) ... in a json it looks like if it was the second column), I hope I made my self understand.

CodePudding user response:

I don't know how do you decide what orderNum of property should be selected, but you can get any property by orderNum using this code

let orderNum=1;
console.log( data[Object.keys(data)[orderNum]]); //  "Player": "Fairline69",...
  • Related