How to put variable within quotes in javascript? [duplicate]

0

I need to make this code work. But whenever I put the variable in place or inside the "" it does not work.

//jvectormap data
var visitorsData = {
"US": 398, //USA
"SA": 400, //Saudi Arabia
"CA": 1000, //Canada
"DE": 500, //Germany
"FR": 760, //France
"CN": 300, //China
"AU": 700, //Australia
"BR": (TOTAL_VISIT_PAIS), //Brazil
"IN": 800, //India
"GB": 320, //Great Britain
"RU": 3000 //Russia
};'

I wanted it to look something like this:

//jvectormap data
var visitorsData = {
"US": 398, //USA
"SA": 400, //Saudi Arabia
"CA": 1000, //Canada
"DE": 500, //Germany
"FR": 760, //France
"CN": 300, //China
"AU": 700, //Australia
(CODE_PAIS): (TOTAL_VISIT_PAIS), //Brazil
"IN": 800, //India
"GB": 320, //Great Britain
"RU": 3000 //Russia
};'

Thank you in advance for your help. :)

    
asked by anonymous 21.11.2015 / 19:09

1 answer

1

As below, putting the variable inside [], without needing to quote quotation marks:

var TOTAL_VISIT_PAIS = '1111';

//jvectormap data
var visitorsData = {
"US": 398, //USA
"SA": 400, //Saudi Arabia
"CA": 1000, //Canada
"DE": 500, //Germany
"FR": 760, //France
"CN": 300, //China
"AU": 700, //Australia
"BR": TOTAL_VISIT_PAIS, //Brazil
"IN": 800, //India
"GB": 320, //Great Britain
"RU": 3000 //Russia
};

var CODE_PAIS = "BR";
var TOTAL_VISIT_PAIS = 111;

var visitorsData = {
"US": 398, //USA
"SA": 400, //Saudi Arabia
"CA": 1000, //Canada
"DE": 500, //Germany
"FR": 760, //France
"CN": 300, //China
"AU": 700, //Australia
[CODE_PAIS]: TOTAL_VISIT_PAIS, //Brazil
"IN": 800, //India
"GB": 320, //Great Britain
"RU": 3000 //Russia
};

Traversing vector items with foreach:

for (var item in visitorsData) {
    console.log(visitorsData[item]);
}

Note: When the variable is of type String does not have to put the quotes, since Javascript already recognizes as having, being of type String where in the assignment of the variable it is placed :

    
21.11.2015 / 19:16