开发者

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;
  }())
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜