简体   繁体   中英

Accessing a JSON object in an array

I've gone through the Lynda courses on JSON and really searched around to solve this bug; however, I guess my question is too specific and I need an example.

JSON

{
  "date": "2017-10-15",
  "league": "NHL",
  "odds": "spreads",
  "status": "200 OK",
  "updated": "2017-10-23 00:07 UTC",
  "games": [
    {
      "away": {
        "rot": "051",
        "odds": {
          "acesport": "-1.5 +178",
          "betphoenix": "-1.5 +177",
          "betus": "-1.5 +170",
          "bookmaker": "-1.5 +180",
          "bovada": "-1.5 +170",
          "dsi": "-1.5 +170",
          "easystreet": "-1.5 +180",
          "jazz": "-1.5 +175",
          "mirage": "-1.5 +180",
          "open": "-1.5 -110",
          "pinnacle": "-1.5 +192",
          "sbg": "-1.5 +175",
          "sia": "-1.5 +150",
          "sportsbet": "-1.5 +240",
          "station": "-1.5 +175",
          "westgate": "-1.5 +175"
        },
        "team": "Boston Bruins",
        "score": 1
      },
      "home": {
        "rot": "052",
        "odds": {
          "acesport": "+1.5 -208",
          "betphoenix": "+1.5 -217",
          "betus": "+1.5 -200",
          "bookmaker": "+1.5 -210",
          "bovada": "+1.5 -200",
          "dsi": "+1.5 -200",
          "easystreet": "+1.5 -210",
          "jazz": "+1.5 -205",
          "mirage": "+1.5 -220",
          "open": "+1.5 -110",
          "pinnacle": "+1.5 -214",
          "sbg": "+1.5 -210",
          "sia": "+1.5 -175",
          "sportsbet": "+1.5 -280",
          "station": "+1.5 -210",
          "westgate": "+1.5 -200"
        },
        "team": "Vegas Golden Knights",
        "score": 3
      },
      "time": "7:05 PM EST",
      "status": "Final"
    }
  ]
}

From what I've gathered from searching and videos I think the code below is close. The issue must be that this inst just a value in an array but another object?

for (i = 0; i <= body.games.length; i++){
    for (var key in body.games[i]) {
        console.log("Game " + (i +1));
        console.log(body.games[i][key].away.odds.acesport);
    }
}

Your post doesn't contain a question. I will assume the questions "How to access an object in an array?" and "Why does my code not work?".

Maybe this example will clarify it for you.

for (var i = 0; i <= body.games.length; i++) {
    var game = body.games[i];
    console.log("Game number " + (i + 1));
    console.log(game.away.odds.acesport);
    for (var key in game) {
        // This will print the strings "away", "home", "time" and "status",
        // but not the values game[key] which are "object", "object",
        // "7:05 PM EST" and "Final".
        console.log("Game property: " + key);
    }
}

You are trying to access body.games[i].away.away.odds.acesport , this wont work. You should try some approaches:

Approach #1

Just get the values of the 2 keys you know have the properties you want.

for (i = 0; i <= body.games.length - 1; i++){
  console.log("Game " + (i +1));
  console.log(body.games[i].away.odds.acesport);
  console.log(body.games[i].home.odds.acesport);
}

Approach #2

Check if the property odds and acesport exists. This is more scalable.

for (i = 0; i <= body.games.length - 1; i++) {
  console.log("Game " + (i +1));
  for (var key in body.games[i]) {  
    var acesport = body.games[i][key].odds && body.games[i][key].odds.acesport;
    if (acesport) console.log(acesport);
  }
}

Approach #3

Now, if you want to try a little cleaner:

body.games.forEach(function(game, index) {
  console.log("Game " + index + 1);
  Object.keys(game).forEach(function (prop) {
    var acesport = game[prop].odds && game[prop].odds.acesport;
    if (acesport) console.log(acesport);
  });
});

Or cleaner yet (if you can use string template and arrow functions):

body.games.forEach((game, index) => {
  console.log(`Game ${index + 1}`);
  Object.keys(game).forEach((prop) => {
    var acesport = game[prop].odds && game[prop].odds.acesport;
    if (acesport) console.log(acesport);
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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