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.
No, JavaScript objects cannot have duplicate keys. The keys must all be unique.
Use . replace() method to replace the Non-ASCII characters with the empty string.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With