Reputation: 2478
I have this JSON. I need to first compare values in teams
with values in html_content.position[i]
where i
should be a loop var, and then if comparison returns true then get the value. See this example:
html_content.position[0][0].toLowerCase() = "navegantes";
Then I should compare with each value in teams
and if there is any key that es equal then I should get the value, taking the example, I should get this value:
"img\/magallanes.jpg"
Can any give me some help here?
Upvotes: 0
Views: 55
Reputation: 6233
The problem is with this code you are using:
html_content.position[0][0].toLowerCase() = "navegantes";
html_content.position
actually is an array of arrays of arrays (though in your example data each inner array is only of length 1... is that always true?), so you need one more bracket operator on there to test the value of html_content.position[i][0][0]
Here is an example that incorporates your JSON data, looks through each team, and finds the corresponding position
: (see below for working JSfiddle demo)
The solution code:
var matches = [];
for(var teamName in json.teams)
{
for(var i = 0, len = json.html_content.position.length; i < len; i++)
{
if(json.html_content.position[i][0][0].toLowerCase() === teamName.toLowerCase())
{
// found a match. do something with it..
matches[matches.length] = {Name: teamName, Value: json.teams[teamName]};
break;
}
}
}
The JSON: (for reference since previously was only published on 3rd party site)
var json = {
"response":true,
"teams":{
"navegantes":"img\/magallanes.jpg",
"tigres":"img\/tigres.jpg",
"caribes":"img\/caribes.jpg",
"leones":"img\/leones.jpg",
"aguilas":"img\/aguilas.jpg",
"tiburones":"img\/tiburones.jpg",
"bravos":"img\/bravos.jpg",
"cardenales":"img\/cardenales.jpg",
"maga":"img\/magallanes.jpg",
"tigr":"img\/tigres.jpg",
"cari":"img\/caribes.jpg",
"leon":"img\/leones.jpg",
"agui":"img\/aguilas.jpg",
"tibu":"img\/tiburones.jpg",
"brav":"img\/bravos.jpg",
"card":"img\/cardenales.jpg"
},
"html_content":{
"position":[
[
[
"Navegantes",
"14",
"10",
"4",
"0"
]
],
[
[
"Tigres",
"14",
"10",
"4",
"0"
]
],
[
[
"Caribes",
"14",
"9",
"5",
"1"
]
],
[
[
"Leones",
"14",
"9",
"5",
"1"
]
],
[
[
"Tiburones",
"13",
"5",
"8",
"4.5"
]
],
[
[
"Aguilas",
"14",
"5",
"9",
"5"
]
],
[
[
"Bravos",
"14",
"4",
"10",
"6"
]
],
[
[
"Cardenales",
"13",
"3",
"10",
"6.5"
]
]
],
"current":[
[
"MAGA",
"CARI",
"7:00 pm",
"PUERTO LA CRUZ"
],
[
"AGUI",
"LEON",
"4:00 pm",
"CARACAS"
],
[
"BRAV",
"TIGR",
"5:30 pm",
"MARACAY"
],
[
"TIBU",
"CARD",
"5:30 pm",
"BARQUISIMETO"
]
],
"next":[
[
"MAGA",
"CARI",
"6:00 pm",
"PUERTO LA CRUZ"
],
[
"AGUI",
"LEON",
"1:00 pm",
"CARACAS"
],
[
"TIBU",
"TIGR",
"5:30 pm",
"MARACAY"
],
[
"BRAV",
"CARD",
"2:00 pm",
"BARQUISIMETO"
]
],
"previous":[
]
}
};
See an example fiddle here: http://jsfiddle.net/VqHpJ/
The example fiddle creates an array of matches
which is a collection of objects representing the team Name
and the Value
for the associated image, and spits them out in a list. (Of course, you didn't say what you want to do once you found a match, so you can adapt this as needed).
This is a fairly typical pattern for matching elements in two arrays (one nested loop), and in my example it assumes that only one match should be found, in which case it will break
out of the nested loop and start looking for the next team. Therefore the performance is O(n^2) in the worst case.
This is a little tricky (but not difficult) because the teams
object does not really have an array of teams, it has specific properties for each team name. Therefore the solution was to iterate over the properties of the teams
object using for(var teamName in json.teams)
. If you are the author of the function that generates the JSON, you may want to consider revising it to instead generate an array of teams, for example:
var json = {
"response":true,
"teams":
[
{ "Name": "navegantes", "ImageUrl": "img\/magallanes.jpg" },
{ "Name": "tigres", "ImageUrl": "img\/tigres.jpg"},
{ "Name": "caribes", "ImageUrl": "img\/caribes.jpg"},
...
]
...
}
Upvotes: 1
Reputation: 1516
Assuming you have a JSON string that you parsed into an object that looks like the following
var json = {"teams":{
"navegantes":"img\/magallanes.jpg",
"tigres":"img\/tigres.jpg",
"caribes":"img\/caribes.jpg",
"leones":"img\/leones.jpg",
"aguilas":"img\/aguilas.jpg",
...
}};
You can iterate using each()
$.each(json.teams, function(i, v){
console.log(i);
console.log(v);
});
Upvotes: 0