hugsbrugs
hugsbrugs

Reputation: 3611

build javascript array from another function

I have a translation function I call like this : _("My text").

I am trying to build an array :

    var data = {
                        _("region"):[
                            {"id": "vignoble","name": _("Vineyard") },
                            {"id": "sousvignoble","name": _("Sub Vineyard") },
                            {"id": "soussousvignoble","name": _("Sub Sub Vineyard") },
                            {"id": "appellation","name": _("Appellation") },
                            {"id": "denomination","name": _("Denomination") }
                        ]};

It works great for translating property "name" : _("My text") but throws a "Syntax Error : Missing : after property id" for first level _("region").

I've tried to replace it by : 1. function(){_("region")} 2. define a variable and replace _("region") by the variable but the name of variable prints and not its value

Any idea would be very appreciated !

Upvotes: 0

Views: 39

Answers (1)

Felix Kling
Felix Kling

Reputation: 816364

You cannot use dynamic property names in object literals. You can assign the property after the object was created, using bracket notation:

var data = {};
data[_("region")] = [...];

Upvotes: 2

Related Questions