1

I am working on a PHP, JS site. I need a dynamic JS array key.

var list = '';

for(i in data)
{
    var list += '<li><a>'+data[i].fieldname_usa+'</a></li>';
}

here I want the key to be dynamic

Ex: var country = "usa"

the key would be fieldname_usa

var country = "uk";

the key would be fieldname_uk

I am trying to achieve this by

 var country = "usa";
 var list += '<li><a>'+data[i].fieldname_+country+'</a></li>';

Because I use different databases for different countries and the filed names would be different in each country .

But seems like it process data[i].fieldname_ as the key. so my finalist put is undefinedusa or undefineduk.

Anton Ohorodnyk
  • 891
  • 5
  • 20
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193

1 Answers1

4

You need a different notation with []

list += '<li><a>' + data[i]['fieldname_' + country] + '</a></li>';

(Please delete the var in front of the line. list is already declared and initialized.)

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392