Reputation: 349
I have a Json with some informations using Ruby on Rails (I generate this through a method in a controller):
pis: [
{
kilometer: 621,
category: "fare",
name: "Guaraçaí",
toll: "false",
lat: -21.0059,
lng: -51.1904,
icon: "/img/icon_fare_map.png",
pedagios: [ ]
},
And the following function get the Json (this show markers in my Google Maps):
function getAllPI(){
$.get("/map_pis", function(data){
$(data.pis).each(function(){
// Adds a marker for each Json item
addMarker(this);
// Show all markers
showMarkers();
})
});
}
I just want to add the marker and show it if my field 'toll' in Json is 'true'. If is 'false' I dont wanna show. How can I do that? I tried with:
if (pis.toll == true ) {
addMarker(this);
}
But that didn't work. I do not have much experience with Json.
Someone can help me? Thanks.
Upvotes: 0
Views: 190
Reputation: 21
In your json, your toll value seems to be a string. Therefore doing
pis.toll == true
will always be false
, as "true"
is not equal to true
Maybe this will work
pis.toll == "true"
Upvotes: 2
Reputation: 5766
You need to define a parameter for your .each
block, which refers to the specific item in the array you're working with. Then you can check if the toll on that is true and if it is, add the marker. I'm not sure if this
is the argument you wanted to pass to that, but I would guess item
is what you need.
function getAllPI(){
$.get("/map_pis", function(data){
$(data.pis).each(function(item){
// Adds a marker for each Json item
if (item.toll) addMarker(item);
// Show all markers
showMarkers();
})
});
}
Upvotes: 1