0

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

1
  • @tabz100 Uncaught ReferenceError: roadmap is not defined Commented Jan 14, 2015 at 3:21

1 Answer 1

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
   }
});

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.