我有一个数组对象:
var checkin_status = [
{"startdate":"2015-01-08",
"totaldays":"3",
"roadmap":[
{"gifttype":"stars","quantity":100,"day":1},
{"gifttype":"stars","quantity":500,"day":3},
{"gifttype":"stars","quantity":1000,"day":10},
{"gifttype":"stars","quantity":1200,"day":20}
]
}];
我使用$.each
显示它们进行查看,现在我如何查看roadmap
中的最后一个对象?因为我必须为最后一个对象提供一些条件。有什么建议吗?谢谢
答案 0 :(得分:2)
//assuming that you already have a better logic to select the roadmap from the checkin_status object
//assign the current roadmap object to a local var before you iterate through it
var roadmap = checkin_status[0].roadmap;
$.each(roadmap, function (index, item) {
//'item' is the object of the current iteration..
if (index === roadmap.length - 1) {
//do something special with the last item
}
});
length - 1
的原因是因为,Javascript中的数组被索引为0。例如,如果您有4个项目,那么最后一个项目是索引3.