我有一个要循环的对象。在我循环对象之前,我决定将值存储在单独的变量中,为了这样做,我使用对象符号来获取值..我对“选项”和“行”进行了未定义-这两个访问有什么问题?
或者任何人告诉我访问这些节点的正确方法。
我目的是:
var ob = {
"DashBoard":[
{"title":"DashBoard"},
{"widget":[{"slide":"To do"},{"slide":"Teamspace"},{"slide":"Recent Activity"}]},
{"activity":[
{"option":[{"show":"Volvo"},{"show":"Benz"},{"show":"Honda"}]},
{"rows":[
{"title":"Advert1", "text":"sample text1", "date":"22-06-2013"},
{"title":"Advert2", "text":"sample text2", "date":"22-06-2014"},
{"title":"Advert3", "text":"sample text3", "date":"22-06-2015"},
{"title":"Advert4", "text":"sample text4", "date":"22-06-2016"},
{"title":"Advert5", "text":"sample text5", "date":"22-06-2017"}
]}
]}
]
}
var title = ob.DashBoard[0], widget= ob.DashBoard[1], option = ob.DashBoard[2]["option"], rows = ob.DashBoard[2]["rows"];
console.log(option); //undefined
console.log(rows); //undefined
我在两种情况下都得到了未定义的结果...
fiddle here
2条答案
按热度按时间gmxoilav1#
读取
ob.DashBoard[2].activity[0]["option"] and ob.DashBoard[2].activity[1]["option"].
您将能够读取您的json
nsc4cvqm2#
使用下面,它应该工作。