Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using non-ASCII character as JavaScript object key [duplicate]

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.

like image 737
Raptor Avatar asked Jun 19 '15 03:06

Raptor


People also ask

Can JavaScript object have same keys?

No, JavaScript objects cannot have duplicate keys. The keys must all be unique.

How do you remove non-ASCII characters?

Use . replace() method to replace the Non-ASCII characters with the empty string.

What is a non-ASCII character?

Non-ASCII characters are those that are not encoded in ASCII, such as Unicode, EBCDIC, etc. ASCII is limited to 128 characters and was initially developed for the English language.

Can we get key from value in JavaScript?

To get an object's key by it's value: keys() method to get an array of the object's keys. Use the find() method to find the key that corresponds to the value. The find method will return the first key that satisfies the condition.


1 Answers

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)

like image 105
Hunter McMillen Avatar answered Nov 14 '22 23:11

Hunter McMillen