Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use Unicode string key In Javascript Object?

I want to use unicode string in Object as key, something as:

var t = {"姓名": "naitong"};

it's ok , t["姓名"] return "naitong"

but

Object.keys({"姓名": "naitong"})

I got " ", a blank string

Anyone knowes why?

Editting:

I install firebug and try it in the console, it works. Acctually i use mozrepl, so that i can editing and run javascript in emacs. So This have something to do with mozrepl

I have confirm that mozrepl support only "7bit safe ASCII", to tranform unicode ,i have to json-encode it in emacs, as:

alert(Object.keys(JSON.parse("{\"\\u59d3\\u540d\":\"naitong\"}")))

This is my first question asked on stackoverflow, and i got quick resp. Thank you all.

like image 404
Naitong Xiao Avatar asked Mar 13 '12 09:03

Naitong Xiao


People also ask

Can JavaScript object keys be string?

Each key in your JavaScript object must be a string, symbol, or number.

Can I use Unicode in JavaScript?

In Javascript, the identifiers and string literals can be expressed in Unicode via a Unicode escape sequence. The general syntax is \uXXXX , where X denotes four hexadecimal digits. For example, the letter o is denoted as '\u006F' in Unicode.

How do you add a Unicode to a string?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.

Can an object be a key in an object JavaScript?

Can you use objects as Object keys in JavaScript? # The short answer is "no". All JavaScript object keys are strings.


1 Answers

Works fine for me in the firebug console:

>>> Object.keys({"姓名": "naitong"})
["姓名"]

Maybe you are trying to display it on a page that uses a different charset which does not contain those symbols.

like image 180
ThiefMaster Avatar answered Nov 08 '22 06:11

ThiefMaster