Search code examples
javascriptvariablesobject-literal

Define object with variables for property names


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?


Solution

  • something like this;

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