How to put variable inside quotes in javascript?

Asked

Viewed 1,283 times

0

I need to make this code work. But every time I put the variable in place or inside "" it doesn’t 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
};`

Thanks for your help. :)

  • 2

    Have you tried visitorsData[CODE_PAIS] = TOTAL_VISIT_PAIS;?

  • I read the your comment, then you want to add to the object visitorsData data that comes from PHP is this?

  • Or you want to add data to that object in PHP directly?

  • And this JS code that you have in question is done in PHP? could you put a loop in PHP to create these properties. Ie reading from the BD do something like js.= $chave.': '.$valor.','; Is that what you want? in this case shows part of PHP to give you a more concrete example

1 answer

1


As follows, by placing the variable in [ ], without the need to quote:

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]);
}

Obs.: When the variable is of the String type it does not have the need to put the quotes, because Javascript already recognizes as having, for being of the String type where in the assignment of the variable it is placed:

  • 1

    I have tried this. The variable TOTAL_VISIT_PAI works normally, but CODE_PAIS does not.

  • Puts the CODE_PAIS variable inside [], as I edited the code.

  • It worked perfectly expensive. Thank you very much.. D

  • Just one more question. how do I loop this code. Type: //jvectormap data var visitorsData = { if (var i=0; i < 10; i++){ [CODE_PAIS_VISIT]: i, //Brazil } }; Note: The above code did not work.

  • As follows: for (var item in visitorsData ) console.log(visitorsData[item]);

  • But this way you talk works the same as the code below? //jvectormap data var visitorsData = { if (var i=0; i < 10; i++){ for (i < 10){ [CODE_PAIS_VISIT]: i, //Brasil }else{[CODE_PAIS_VISIT]: i//Brasil} } };

  • No, the way I said it goes through item by item in the visitorsData variable.

  • So how do I loop an item within the visitorsData variable?

  • You can access the value directly without loop so for example: visitorsData['FR']

  • 1

    @Pablomarconyf [CODE_PAIS]: TOTAL_VISIT_PAIS, will not work in Internet Explorer or other less modern browsers.

  • I will explain how the system works so you can help me. I have an index.php page. It has a loop that collects row by row from the infor_visit table of the database and inserts in the variables that are sent to the Dashboard.js file where you have the above code.

  • What I want is for the visitorsData variable to store the same amount of row and information as in my table. NOTE: The number of rows in the database is not fixed.

  • 1

    @Pablomarconyf If you have additional questions, unrelated to the current question, please ask new questions. Avoid extensive discussions in comments.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.