1

Sorry, this is probably a duplicate question, but how can I iterate over a list in Javascript inside another 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!

1 Answer 1

1

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;
  }())
);
Sign up to request clarification or add additional context in comments.

4 Comments

I like this! Didn't even know you could do that! scraps his own solution ha
yeah, it's an amazing feature of Functional scope.
I'm familliar with for(;;){} syntax in JS but not for(,,,){}. Is that a typo?
the first comma is saying x = 0 AND y = length, so it keeps a comma delimeter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.