问题
not well versed in Backbone, so understand if I don't explain this completely.
I have a collection I am trying to retrieve. Here is an example of the JSON:
{
"id" : "section-one",
"href" : "section-one-baseball",
"divisions" : [
{
"name": "Orioles",
"division" : "AL East"
}
]
}
My problem is that I can't figure out how to get to the sections name. When I put this in the console:
BaseballTeams.models[0].get("divisions")
I get back an Object with the name and the division. But if I put this in:
BaseballTeams.models[0].get("divisions.name")
I get undefined. This is a matter more likely that I am not sure how to retrieve the name of the Object, and that is why I am looking for some help please. Thank you in advance.
回答1:
try this.
BaseballTeams.models[0].get("divisions")[0].name
1.BaseballTeams.models[0].get("divisions") return following
[
{
"name": "Orioles",
"division" : "AL East"
}
]
[] is meaning array
2.BaseballTeams.models[0].get("divisions")[0] return following
{
"name": "Orioles",
"division" : "AL East"
}
{} is meaning object
then you can get value you want like below
BaseballTeams.models[0].get("divisions")[0].name
or
BaseballTeams.models[0].get("divisions")[0]["name"]
来源:https://stackoverflow.com/questions/26774669/backbone-collection-get-object