Вопрос

I have a problem in declaring a inicialiting an object. When I define an object and pass by reference a string does not recognize me and fails. The object is as follows:

markerGroups = {"america": [], "europa": [], "asia": [],"africa": [], "oceania": [] };

Well, it works correctly, but if I change, for example, "america" ​​putting var amer = "america"​​, like this:

var amer = "america"; 
markerGroups = {amer: [], "europa": [], "asia": [],"africa": [], "oceania": [] };

It does not work. What i have to do for resolve this?

Это было полезно?

Решение 2

something like this;

var markerGroups = {}
var amer = "america"; 
markerGroups[amer] = [];

Другие советы

In JavaScript, you don't need to quote your object keys. So amer: [] is creating the literal key "amer".

You need to use the [] method to do this:

var amer = "america"; 

markerGroups = {...};
markerGroups[amer] = [];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top