1

I am trying to read schedule data from a JSON file called js/schedule.json which looks like:

[
{"date":"12/06/2014","day":"Thursday","kick-off":"21:00","team1":"Brazil","team2":"Croatia","group":"A","stage":"group stages","broadcaster":"ITV"},
{"date":"13/06/2014","day":"Friday","kick-off":"17:00","team1":"Mexico","team2":"Cameroon","group":"A","stage":"group stages","broadcaster":"ITV"}

...    
]

I want to iterate through each item in the JSON object and only return the match fixture where the "date" matches a previously set variable called: today.

The jQuery I'm using is the following:

function getArray(){
    return $.getJSON('js/schedule.json');
}

getArray().done( function(json) {
    console.log(json); // show the json data in console
    var _len = json.length;
    var fixture;


    //loop through json and match today's date with match-date
    for (var i in json) {
        fixture = json[i];
        if (fixture.date == today) {
            //print out today's schedule here
            console.log(fixture.team1 + " Vs. " + fixture.team2);
        }
    }

});

However, what I get as an output in the Console is [Object, Object, Object, Object, Object, Object, Object], there are 7 items in the object so this is the correct amount for the time being, however I can't get the console to show the fixture.team1 + fixture.team2

I've checked various posts which have similar problems such as JSON returning [object Object] but these don't quite fix it.

Any help would be fantastic. Cheers!

As pointed out the problem here was with the value of today not matching the value in the date. After adding a preceding "0" to months/days < 10 the code worked.

Community
  • 1
  • 1
dubblebee
  • 83
  • 1
  • 3
  • 9
  • 2
    What is the value of `today` exactly ? Seems to me that your `date` field has a simple string value, so if your `today` variable is a Javascript date, you will need to parse it accordingly. – wrousseau Jun 09 '14 at 15:15
  • 1
    Have a look at [Why is using “for…in” with array iteration such a bad idea?](http://stackoverflow.com/q/500504/1048572), though that is probably not the reason for your problem – Bergi Jun 09 '14 at 15:18
  • So it seems there's no issue with anything except for performing the date comparison. As such, you should ask specifically about that issue, or rather search for the answer first. – cookie monster Jun 09 '14 at 15:27
  • @dubblebee Cookie monster is totally right. Comment out if (fixture.date == today) {} and console.log(fixture.team1 + " Vs. " + fixture.team2); displays exactly what you expect. You need to handle your dates. See doc [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). – Threadid Jun 09 '14 at 15:45
  • @wrousseau for test purposes `today` is set to "12/6/2014". Is the omission of a leading 0 in "/6/" is causing the data mismatch? – dubblebee Jun 09 '14 at 15:51
  • @Bart OK Yes you are correct. That now displays what I'm expecting , so I need to correct my date handling. Thanks all! – dubblebee Jun 09 '14 at 15:56

2 Answers2

1

You're using for ... in when you want a normal for loop (given your data is an array of objects, not an object with a lot of unknown properties/keys). With that said, try something like:

getArray().done(function(json){
    // ...

    for (var i = 0; i < json.length; i++){
       var o = json[i]; // {"date":"12/06/2014","day":"Tursday",...}
       // now perform a check against `o['date']` or `o.date` and return the
       // matching result
    }
});
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • Your way of iterating is better, but I'm not seeing how it addresses the issue in the question. He's already performing a check against the `.date` property. – cookie monster Jun 09 '14 at 15:25
1

Output

[Object, Object, Object, Object, Object, Object, Object]

is an output of

console.log(json); // show the json data in console   

You have never got output of

console.log(fixture.team1 + " Vs. " + fixture.team2);

because your condition in if-condition is always false in your example. So, you should specify properly value for today variable.

If you want to log json object, you can use JSON.stringify function

console.log(JSON.stringify(json));
Ilya
  • 29,135
  • 19
  • 110
  • 158