8

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.

5
  • 2
    Is this JavaScript? That is not a proper JavaScript object: it's using => instead of : Commented Jun 19, 2015 at 3:03
  • 2
    obj.ア works for me in the Chrome developer console.. Commented Jun 19, 2015 at 3:03
  • @jasonscript sorry, that's a typo, now corrected. Commented Jun 19, 2015 at 3:05
  • @Blorgbeard I'm using CasperJS. It said Parse Error ... Commented Jun 19, 2015 at 3:06
  • 1
    You can only use dot notation if the property name is a valid variable name. Commented Jun 19, 2015 at 3:18

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

Comments

5

You can use property accessors:

obj['ト']

Example:

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

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

MDN: Property Accessors

Comments

-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>

2 Comments

thanks, but this results in parse error in Casper JS.
language attribute is deprecated for years. And CasperJS syntax is slightly different than normal JS.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.