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,589
  • 4
  • 34
  • 50
Raptor
  • 53,206
  • 45
  • 230
  • 366

3 Answers3

8

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
  • 59,865
  • 24
  • 119
  • 170
5

You can use property accessors:

obj['ト']

Example:

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

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

MDN: Property Accessors

Cymen
  • 14,079
  • 4
  • 52
  • 72
-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
  • 3,588
  • 3
  • 29
  • 54