1

In the following code I am accessing the cookieCode in the var MasterTmsUdo but I am unable to access this variable. Can anyone tell me how can I access this variable inside var MasterTmsUdo?

var cookieCode="True";
console.log(cookieCode);
var MasterTmsUdo = { 'CJ' : { 'CID': ' 897415', 'TYPE': '894', 'AMOUNT' : '35.00', 'OID' : "115", 'CURRENCY' : 'USD','FIRECJ' : cookieCode, } }; 
Alexis Tyler
  • 1,394
  • 6
  • 30
  • 48
Azhar Ahmad
  • 183
  • 12

2 Answers2

3

It's a property of the CJ object, therefore you can access it via:

MasterTmsUdo.CJ.FIRECJ

Therefore you could have:

var cookieCode="True";
console.log(cookieCode);
var MasterTmsUdo = { 'CJ' : { 'CID': ' 897415', 'TYPE': '894', 'AMOUNT' : '35.00', 'OID' : "115", 'CURRENCY' : 'USD','FIRECJ' : cookieCode } };
console.log(MasterTmsUdo.CJ.FIRECJ);
Curtis
  • 101,612
  • 66
  • 270
  • 352
  • I dont want to access the FIRECJ elements. I have to set the FIRECJ Value equal to cookieCode Which is javascript variable.How can i do that? – Azhar Ahmad Sep 07 '15 at 14:57
  • @user3480945 You are already passing a reference to `cookieCode` in your declaration. – Curtis Sep 07 '15 at 14:59
  • but it is not take the value of cookieCode.It prints the as FIRECJ:cookieCode while it should be FIRECJ:"True" ? – Azhar Ahmad Sep 07 '15 at 15:01
  • @user3480945 See my update. If you run my code snippet do you not see True? NB I've also removed a trailing comma in your JSON object which may have been causing issues in certain browsers – Curtis Sep 07 '15 at 15:10
  • I have a basic question why it is not print the value of cookieCode at FIRECJ:cookieCode ? – Azhar Ahmad Sep 07 '15 at 15:15
  • @user3480945 What script are you writing exactly? – Curtis Sep 08 '15 at 07:20
0

Access the variable by using the following code:

var FireCJ = MasterTmsUdo['CJ']['FIRECJ'];

OR

var FireCJ = MasterTmsUdo.CJ.FIRECJ;

You can then alert or console.log the code and it will return the string "True". FireCJ can also be used in other code to make some IF statements for example
Small note: You can either use a boolean type (true or false) instead of a string type containing 'True'.

IF statement with boolean:

if(FireCJ) {
    // TRUE
} 

IF statement with your code:

if(FireCJ == 'True') {
    // TRUE
}
Wesley
  • 21
  • 4