本文介绍了如何访问对象键中有空格的 JavaScript 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的 JavaScript 对象:
I have a JavaScript object that looks something like this:
var myTextOptions = {
'cartoon': {
comic: 'Calvin & Hobbes',
published: '1993'
},
'character names': {
kid: 'Calvin',
tiger: 'Hobbes'
}
}
我可以使用 myTextOptions.cartoon.comic
或其他工具轻松访问 cartoon
的属性.但是,我无法获得访问 kid
的正确语法.我尝试了以下但没有运气:
I can access the properties of cartoon
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
推荐答案
使用 ECMAscripts 括号表示法":
Use ECMAscripts "bracket notation":
myTextOptions[ 'character names' ].kid;
无论哪种方式,您都可以使用该符号,阅读 &写作.
You can use that notation either way, reading & writting.
欲了解更多信息,请阅读此处:
For more information read out here:
这篇关于如何访问对象键中有空格的 JavaScript 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!