I am trying to make the following function run but I have stucked badly. I have an object "gidsAll" which I want to compare with each feature: feature.attributes.__gid.
The function is used in Openlayers for styling purpose of the features (points and lines). It is executed for each "feature.attributes.__gid" and what I want is to simple compare this value with all the values inside the "gidsAll". If the value matches one of the value in "gidsAll" then return green otherwise return red.
I think I need to use a loop somewhere but I can not make it work.
var styleContext = {
getColor: function (feature) {
//alert(typeof(feature));//object
// for (key in gidsAll){
if (Math.round(feature.attributes.__gid) == Number(gidsAll)){
return "green";
} else {
return "red";
}
//}
}
};
You can use Object.keys()
to loop over all the objects properties.
Object.keys
returns all the enumerable object's own properties of the object it receives.
var styleContext = {
getColor: function (feature) {
var objectKeys = Object.keys(gidsAll);
for (var i = 0; i < objectKeys.length; i++)
{
if (Math.round(feature.attributes.__gid) === parseInt(gidsAll[objectKeys[i]], 10))
{
return "green";
}
}
return "red"; //nothing has been found
//}
}