7

I have the following object:

var obj = {
  'ア' : 'testing',
  'ダ' : '2015-5-15',
  'ル' : 123,
  'ト' : 'Good'
};

How do I access the values by its non-ASCII key (it's a Japanese character in this case)?

Can't use obj.ア or obj.'ア' for sure, which will give JavaScript parse error.

Archy Will He 何魏奇
  • 9,367
  • 4
  • 30
  • 49
Raptor
  • 51,208
  • 43
  • 217
  • 353

3 Answers3

7

You can use a subscript to reference the object:

> var obj = {
  'ア' : 'testing',
  'ダ' : '2015-5-15',
  'ル' : 123,
  'ト' : 'Good'
};
> undefined
> obj['ア']
> "testing"

You should also not that object keys and values in JavaScript objects are separated by :(colons) not => (fat commas)

Hunter McMillen
  • 56,682
  • 21
  • 115
  • 164
5

You can use property accessors:

obj['ト']

Example:

var obj = {
  'ア': 'testing',
  'ダ': '2015-5-15',
  'ル': 123,
  'ト': 'Good'
};

console.log(obj['ト']);
> Good

MDN: Property Accessors

Cymen
  • 13,561
  • 4
  • 49
  • 70
-2

How about this one:

    <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
    <script language=javascript>
        var obj = {
'ア':'testing',
'ダ':'2015-5-15',
'ル':123,
'ト':'Good'
};
alert(obj.ア);
</script>
</body>
</html>
The KNVB
  • 2,684
  • 1
  • 23
  • 36