To reduce my last question (it was a bit complicated). Is it possible to change the names of "array objects" dynamically?
I have a list of variables (structure must stay like this way):
var markers = []
var markerHouse = ... markers.push(markerHouse);
var markerAnimal = ... markers.push(markerAnimal);
var markerCar = ... markers.push(markerCar);
// aso.
I tried lots of ways to change the array object names, like this one:
var NewMarkers = "markerHouse, markerAnimal"; // string content, generated by a function
var NewMarkersArray = NewMarkers.split(","); // create array of this string
var NewGroup = L.layerGroup([NewMarkersArray]); // request for array of objects
The result of NewGroup
is:
L.layerGroup(["markerHouse", "markerAnimal"]);
And I get a "TypeError: cannot use ‘in’ operator to search for "_leaflet_id" in "markerHouse" …
But what I need is:
L.layerGroup([markerHouse, markerAnimal]);
4
Answers
Do you need eval?
Using eval() as suggested in other questions is considered a bad practice.
The most straightforward way to retrieve a global variable by it’s string name is from ‘window’. A global variable is actually a property of the ‘window’ object.
So you could do:
And with dynamic variable names:
See snippet below:
Still, if you have the luxury of refactoring the initial code, the best practice would be to store the variables by name on creation e.g.:
Keep your values in an object:
Then:
The
allMarkers
object contains the actual values corresponding to the named variables in your original code. They can be accessed by name dynamically because objects in JavaScript inherently provide that feature. The list of marker names can therefore be transformed into a list of the actual values with that simple.map()
call.If you really can’t change what you’ve got, then I’d probably maintain a map of keys for every variable.
Or even nicer:
Then:
If that’s infeasible, then you’ll have to use eval or window[‘markerHouse’]. Those both feel terrible though and suggest has gone wrong somewhere along the way.