【发布时间】:2018-02-01 02:12:30
【问题描述】:
我正在尝试了解有关循环的更多信息并尝试访问 JSON 格式的对象属性。
我的 JS 是:
var movies = {
"Black Panther" : {
"title" : "Black Panther",
"theatricalrelease" : "2/16/2018"
},
"Infinity War" : {
"title" : "Avengers: Infinity War",
"theatricalrelease" : "5/4/2018"
},
"Captain Marvel" : {
"title" : "Captain Marvel",
"theatricalrelease" : "TBA"
}
}
console.log(movies); //logs correctly
console.log(movies.length); //logs undefined
for (var i = 0; i < movies.length; i++) {
console.log(movies[i]); // doesn't log anything
}
如何访问 title 和 theatricalrelease 等对象属性?
【问题讨论】:
-
那不是数组。
-
你应该首先考虑使用 JSON.parse 它会让你的生活更轻松,然后你可以像在对象文字上那样使用函数
标签: javascript json loops