How can a $.each object index - 1 ?
Because obj[i - 1].day doesn't work
var checkin_status = [{
"startdate": "2015-01-08",
"totaldays": "4",
"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
}, {
"gifttype": "stars",
"quantity": 2200,
"day": 30
}]
}];
var checkin_info = checkin_status[0].roadmap;
$.each(checkin_info, function (i, obj) {
var sum_day = obj[i - 1].day + obj.day;
var middle_day = sum_day / 2;
console.log(middle_day);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
- the
sum_dayoutput should be[4,13,30,50] - divide into 2 , new output should be
[2,6.5,15,50]this is all middle day between[1,3,10,20,30]
obj[i - 1].dayneeds to becheckin_info[i - 1].dayand you'll need to skip the first item for it to work.checkin_info[i - 1].daywil getCannot read property 'day' of undefinedias an index toobj? Theobjargument will have the same value ascheckin_info[i].objis the current element you're iterating over (as in the current object fromroadmap) - that cant be an array. as @nnnnnn just said too!