I need to create dynamic variable name in js inside for loop
var counter = 0;
for(i=0; i<location.length; i++) {
...
var "marker_" + counter = new google.maps.Marker({
But when i expected to have variables marker_0,marker_1,marker_2,... i had this error
Error: SyntaxError: missing variable name
Source Code:
var "marker_" + counter = new google.maps.Marker({
I need to create dynamic variable name in js inside for loop
var counter = 0;
for(i=0; i<location.length; i++) {
...
var "marker_" + counter = new google.maps.Marker({
But when i expected to have variables marker_0,marker_1,marker_2,... i had this error
Error: SyntaxError: missing variable name
Source Code:
var "marker_" + counter = new google.maps.Marker({
Share
Improve this question
edited Jan 21, 2013 at 16:26
Jamiec
136k15 gold badges141 silver badges199 bronze badges
asked Jan 21, 2013 at 16:22
Yasser MoussaYasser Moussa
2,3296 gold badges27 silver badges39 bronze badges
4
- You'll want to use eval() to make things work that way. However, go with the array solution, IMO. – DWright Commented Jan 21, 2013 at 16:25
- I don't see a point of making a dynamic variable in a loop like that since the context will only be in the scope of the current iteration. If you're looking to reuse outside the loop you're going to need to use an array. – Gabe Commented Jan 21, 2013 at 16:25
- This has nothing to do with PHP. I don't even see how this has to deal with jQuery, but I'll let the JS pros handle that. – nickb Commented Jan 21, 2013 at 16:26
- I removed the [jQuery] tag - this has nowt to do with jQuery. – Jamiec Commented Jan 21, 2013 at 16:28
3 Answers
Reset to default 11Use an array:
var marker = [];
for (i=0; i < location.length; i++) {
marker[counter] = new google.maps.Marker({
Well, "Use an array" is undoubtably the right answer here, however if you really want dynamic variables you need to decide the scope of them - the default would be window
and then you could do this:
var counter = 0;
for(i=0; i<location.length; i++) {
...
window["marker_" + counter] = new google.maps.Marker({
This can now be accessed with the same square bracket notation
window["marker_0"]...
or the dot notation
window.marker_0
Since this question is used as a duplicate target:
Use an array is good advice if the information you want to access is keyed numerically. If it isn't, you have two options:
Use an object and dynamic property access
Use a
Map
(ES2015+)
Using an object
You can access object properties using a string (or, in ES2015+, a Symbol) via brackets notation, so you can store information in an object to access later using strings/Symbols:
var theData = Object.assign(Object.create(null), {
x: "ecks",
y: "why"
});
var key = Math.random() < 0.5 ? "x" : "y";
console.log("key = " + key + ", theData[key] = " + theData[key]);
Using a Map
You can use a Map instead of an object even with strings or Symbols as keys, but unlike objects, Map keys can be any JavaScript value (except negative 0, but hey, good enough):
const theData = new Map([
["x", "ecks"],
["y", "why"]
]);
const key = Math.random() < 0.5 ? "x" : "y";
console.log("key = " + key + ", theData.get(key) = " + theData.get(key));
That example uses strings, but again, Map keys can be any type. A Map key can even be an object.