我有一个数组对象:
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
?因为我必须给最后一个对象一些条件。有什么建议吗?谢谢
//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。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句