-1

I have a problem with using ${'variable'+i} in a loop function. My task is to call each function from a loop. I read about template literal but I can't find anything regarding to my problem.

I would like to use

${'variable'+i} // (it works in php but not in javascript)

I have tried to use this ` but it didn't work too.

Here is my code:

        var call = new XMLHttpRequest();
call.onreadystatechange = function(){
    if(this.readyState == 4 && this.status == 200){
        var myObj = JSON.parse(this.responseText);
        var jumlah = myObj.jumlah;
        var varray = new Array();
        for(i=1; i<=jumlah; i++){
            varray.push(`${"myObj.namagame"+i}`);                       
        }
    }
}
    call.open("GET","'.$GLOBALS['dirfile'].'/core/update.php?f=search&q="+str,true);
    call.send();

Here is my JSON result

{"jumlah":3,"namagame1":"ark survival evolved","namagame2":"agents of mayhem","namagame3":"age of mythology"}

So then, I would like to get myObj.namagame1,myObj.namagame2,myObj.namagame3 that is why I use a loop function to get them then write them to the document.

Would you please to help me, I am sorry if my question is such a dumb one. Thank you

anova01
  • 93
  • 1
  • 10
Bobby
  • 67
  • 2
  • 8

2 Answers2

1

You are using template literals in JavaScript as per my understanding. Please try following changes in your code. Hope it will work.

  1. use myObj[namagame${i}] instead of ${"myObj.namagame"+i}

With Template String Literals:

call.onreadystatechange = function(){
            if(this.readyState == 4 && this.status == 200){
                var myObj = JSON.parse(this.responseText);
                var jumlah = myObj.jumlah;
                var varray = new Array();
                for(i=1; i<=jumlah; i++){
              varray.push(myObj[`namagame${i}`]);                        
                }
            }
        }

If we want to try with out template string literal you can go with following code.

call.onreadystatechange = function(){
            if(this.readyState == 4 && this.status == 200){
                var myObj = JSON.parse(this.responseText);
                var jumlah = myObj.jumlah;
                var varray = new Array();
                for(i=1; i<=jumlah; i++){
              varray.push(myObj["namagame"+i]);                        
                }
            }
        }
Ravi Teja Kumar Isetty
  • 1,559
  • 4
  • 21
  • 39
0

Taken from the documentation on Template Literals

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them

For example:

var myVar = 123;

var string = `The value of myVar is ${myVar}`;
var string2 = "The value of myVar is " + myVar;

// both of our new strings are identical

console.log(string);
console.log(string2);

In your example you want something like this:

for(i = 1; i <= jumlah; i++){
     varray.push(myObj[`namagame${i}`]);                       
}
Christopher Moore
  • 3,071
  • 4
  • 30
  • 46