Reputation: 3423
I am trying to modify my function to accept a different number of arguments, so for example the arguments could be (poly1, poly2) or (poly1, poly2, poly3) and so on.
The problem I have is that I'm not sure how to increment the variable name
How can I turn the function below into a function that will accept a number of different arguments and loop through them. I would prefer a functional way to do this rather than using a for loop if possible
function UseWicketToGoFromGooglePolysToWKT(poly1, poly2, poly3, poly4) {
var wicket = new Wkt.Wkt();
wicket.fromObject(poly1);
var wkt1 = wicket.write();
wicket.fromObject(poly2);
var wkt2 = wicket.write();
wicket.fromObject(poly3);
var wkt3 = wicket.write();
wicket.fromObject(poly4);
var wkt4 = wicket.write();
return [wkt1, wkt2, wkt3, wkt4];
}
My attempt
function UseWicketToGoFromGooglePolysToWKT(...args) {
args.map(item, i => {
wicket.fromObject(`poly${i}`);
var wkt + i = wicket.write();
})
Upvotes: 1
Views: 65
Reputation: 72857
You were close.
You just needed to return the result from .write()
, and return the array resulting from .map()
.
You were also missing the new Wkt.Wkt();
call:
function UseWicketToGoFromGooglePolysToWKT(...args) {
var wicket = new Wkt.Wkt(); // this was missing
return args.map(item => { // return this. Also, the `i` isn't necessary.
wicket.fromObject(item); // pass `item` here.
return wicket.write(); // return instead of variable assignment
});
}
Upvotes: 3