I add items to my array with this code.
var allItems = [];
$.getJSON(url, function(data) {
$.each(data, function(i) {
allItems.push({
theTeam: data[i]["TeamName"],
thePlayer: data[i]["TeamPlayer"],
});
});
}
And if i use this code
$.each(allItems, function (i, val) {
console.log(val.theTeam);
});
Resultning in printing all data on position theTeam. Like First row in console AC Milan Second row in console Inter. But i want to print out a specific position in the array. I tried something like console.log(val[0].theTeam);
But it gives me error Cannot read property ‘theTeam’ of undefined.
I want in this case my result to be only AC Milan.
Update inculded Json data
[{
"TeamName": "AC Milan",
"TeamPlayer": "Mange" },{
"TeamName": "Inter",
"TeamPlayer": "Daniel"}]
val is a variable that is local to the $.each() method call. You have a global array called allItems — not val. Do allItems[0].theTeam.
And if that answer is not inline with what you’re asking, then please be more specific. Your question is a little vague, so I’ve done my best to interpret what you’re looking for. Cheers.