访问密钥空间的javascript对象 [英] access javascript object with space in key
本文介绍了访问密钥空间的javascript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个javascript对象看起来像这样:
I have a javascript object that looks something like this:
myTextOptions: {
'cartoon': {
comic: 'Calvin & Hobbes',
published: '1993'
},
'character names': {
kid: 'Calvin',
tiger: 'Hobbes'
}
}
我可以使用轻松访问卡通属性myTextOptions.cartoon.comic
或者其他什么。但是,我无法获得访问 kid
的语法。我试过以下没有运气:
I can access the cartoon properties easily using myTextOptions.cartoon.comic
or whatever. However, I haven't been able to get the syntax right for accessing kid
. I've tried the following with no luck:
myTextOptions.character names.kid
myTextOptions."character names".kid
myTextOptions.character\ names.kid
myTextOptions.'character names'.kid
myTextOptions.["character names"].kid
myTextOptions.character%20names.kid
推荐答案
使用ECMA脚本括号表示法:
Use ECMAscripts "bracket notation":
myTextOptions[ 'character names' ].kid;
您可以使用该表示法,阅读和放大写作。
You can use that notation either way, reading & writting.
有关详细信息,请参阅此处:
For more information read out here:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
这篇关于访问密钥空间的javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文