admin管理员组文章数量:1134562
I have a JavaScript object that looks something like this:
var myTextOptions = {
'cartoon': {
comic: 'Calvin & Hobbes',
published: '1993'
},
'character names': {
kid: 'Calvin',
tiger: 'Hobbes'
}
}
I can access the properties of cartoon
easily using myTextOptions.cartoonic
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
I have a JavaScript object that looks something like this:
var myTextOptions = {
'cartoon': {
comic: 'Calvin & Hobbes',
published: '1993'
},
'character names': {
kid: 'Calvin',
tiger: 'Hobbes'
}
}
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
Share
Improve this question
edited Mar 26, 2019 at 13:13
Ed The ''Pro''
95912 silver badges22 bronze badges
asked Nov 29, 2011 at 21:28
juliojulio
6,72815 gold badges64 silver badges82 bronze badges
2
- In Google Chrome, if you go to inspect element and then hover over the json file data sets, each individual data set will have a tooltip appear showing it's path and it also gives you the option to copy the path into your clipboard. Just an FYI. – Simon Suh Commented Jul 15, 2019 at 13:29
- more... When I type myTextOptions.character%20n.kid returns 'NaN'. The last property name (kid) doen't matter, should be any other. I'm using FireFox Quantum 8.3.0esr (64-bits) on Debian 9 – Daniel Bandeira Commented May 27, 2020 at 13:09
4 Answers
Reset to default 305Use ECMAscript's "bracket notation":
myTextOptions[ 'character names' ].kid;
You can use that notation either way, reading & writing.
For more information read here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
Properties of JavaScript objects can also be accessed or set using a bracket notation (for more details see property accessors). Objects are sometimes called associative arrays since each property is associated with a string value that can be used to access it. So, for example, you could access the properties of the myCar object as follows:
myCar['make'] = 'Ford';
myCar['model'] = 'Mustang';
myCar['year'] = 1969;
For more, read on at Working with JS Objects.
So in your case it's myTextOptions['character names'].kid;
We can also do this by -
myTextOptions[ 'character names' ]['kid']
;
This is useful when we have consecutive keys which consist of space.
Let me share my solution here I am using VueJs with type script.
I got following json to parse and display in UI
{
"Brand": "MAMA",
"Variety": "Instant Noodles Coconut Milk Flavour",
"Style": "Pack",
"Country": "Myanmar",
"Stars": 5,
"Top Ten": "2016 #10"
},
I have created the following model interfere in Typescript
export interface Resto {
Brand: string;
Variety: string;
Style: string;
Country: string;
Stars: any;
Top_Ten: string;
}
I have called the API as:
public async getListOfRestos() {
return (await fetch(
`http://starlord.hackerearth.com/TopRamen`,
{
method: "get",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
body: undefined
}
)) as IFetchResponse<Resto[]>;
}
Used in JSX like:
<div class="parent" v-for="(x,i) in listOfRestos" :key="i">
<div>{{x.Brand}}</div>
<div>{{x.Variety}}</div>
<div>{{x.Style}}</div>
<div>{{x.Country}}</div>
<div>{{x.Stars}}</div>
<div>{{x['Top Ten']}}</div>
</div>
Same can also work in React and Angular.
本文标签: How can I access a JavaScript object which has spaces in the object39s keyStack Overflow
版权声明:本文标题:How can I access a JavaScript object which has spaces in the object's key? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736805166a1953654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论