Iterating over json object without using eval?
Sorry, this is probably a duplicate question, but how can I iterate over a list in Javascript inside anoth开发者_如何转开发er object without using eval()?
See pseudocode in CAPITALS below:
polygon = polygon['coordinates']; //list object
var polygon = new CM.Polygon([
FOR POLY IN POLYGON {
new CM.LatLng(poly[1], poly[0]),
}
]);
Obviously, I don't want a real for-loop inside the CM.Polygon object (a CloudMade map object), what I want is simply to output each LatLng in the list in turn.
Thanks!
Why don't you want to use a real for loop? My suggestion would be to use a self-executing function eg:
polygon = polygon['coordinates']; //list object
var polygon = new CM.Polygon(
(function(){
var oput = [], x, y;
for ( x=0,y=polygon.length ; x<y ; x++){
oput.push(new CM.LatLng(polygon[x][1],polygon[x][0]));
}
return oput;
}())
);
精彩评论