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.
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.
-
2
What is the value of
today
exactly ? Seems to me that yourdate
field has a simple string value, so if yourtoday
variable is a Javascript date, you will need to parse it accordingly. – wrousseau Commented Jun 9, 2014 at 15:15 - 1 Have a look at Why is using “for…in” with array iteration such a bad idea?, though that is probably not the reason for your problem – Bergi Commented Jun 9, 2014 at 15:18
- So it seems there's no issue with anything except for performing the date parison. As such, you should ask specifically about that issue, or rather search for the answer first. – cookie monster Commented Jun 9, 2014 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. – Threadid Commented Jun 9, 2014 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 Commented Jun 9, 2014 at 15:51
2 Answers
Reset to default 1Output
[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));
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
}
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745163755a4614522.html
评论列表(0条)