Backbone Collection get Object

守給你的承諾、 提交于 2020-01-25 15:36:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!