1

Okay, I have this code

localStorage.lang = "th"
var k;
switch(localStorage.lang){
    case "th":
        k = "NameThai";
        break;
    case "en":
        k = "NameEnglish";
        break;
}

$("#test").html(some.object.properties.k);

If localStorage.lang is 'th', I expect k to be "NameThai", and $("test").html is set to some.object.properties.NameThai

And if If localStorage.lang is 'en', I expect k to be "NameEnglish", and $("test").html is set to some.object.properties.NameEnglish

I know my code is wrong, since k in $("#test").html(some.object.properties.k); does not refer to variable k, but refers to object k instead.

Are there any way to achieve this?

srakrn
  • 352
  • 2
  • 16

3 Answers3

3

yes use bracket notation, like this..

$("#test").html(some.object.properties[k]);
tanaydin
  • 5,171
  • 28
  • 45
2

This will do the trick:

  $("#test").html(some.object.properties[k]);
gbalduzzi
  • 9,356
  • 28
  • 58
2

Try this

$("#test").html(some.object.properties[k]);
Sabith
  • 1,628
  • 2
  • 20
  • 38