Reputation: 2979
I have an array object:
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}
]
}];
I use $.each
to display them to view, now how can I check last object in roadmap
? Because I have to give some condition for the last object. Any suggestion to do that ? Thanks
Upvotes: 0
Views: 40
Reputation: 1971
//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
}
});
The reason for length - 1
is because, arrays are indexed by 0 in Javascript. For ex., if you have 4 items, then the last item is index 3.
Upvotes: 2