0

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>

  1. the sum_day output should be [4,13,30,50]
  2. divide into 2 , new output should be [2,6.5,15,50] this is all middle day between [1,3,10,20,30]
7
  • It will throw an error on your first item ("day":1) as it tries to access an element that doesn't exist (there is no item before your first item) Commented Jan 14, 2015 at 9:30
  • Surely obj[i - 1].day needs to be checkin_info[i - 1].day and you'll need to skip the first item for it to work. Commented Jan 14, 2015 at 9:35
  • @Jamiec use checkin_info[i - 1].day wil get Cannot read property 'day' of undefined Commented Jan 14, 2015 at 9:37
  • 2
    Why are you trying to use i as an index to obj? The obj argument will have the same value as checkin_info[i]. Commented Jan 14, 2015 at 9:40
  • but obj is the current element you're iterating over (as in the current object from roadmap) - that cant be an array. as @nnnnnn just said too! Commented Jan 14, 2015 at 9:40

3 Answers 3

2

You dont need an each here at all, you're mapping one array to another, ive split this into two operations below to demonstrate but it does not need to be. It can all be done in one map:

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;
var result1 = checkin_info.slice(1,checkin_info.length).map(function(e,i){
    return checkin_info[i].day + e.day;
});
console.log(result1); // logs [4,13,30,50]

var result2 = result1.map(function(e){
    return e/2;  
});
console.log(result2); // logs [2,6.5,15,50]

this can of course all be shortened to

var checkin_info = checkin_status[0].roadmap;
var result = checkin_info.slice(1,checkin_info.length).map(function(e,i){
    return (checkin_info[i].day + e.day)/2;
});

If you really must have an $.each you can use some of the same principles - slice off the first element:

$.each(checkin_info.slice(1,checkin_info.length), function (i, obj) {
    var sum_day = checkin_info[i].day + obj.day;
    var middle_day = sum_day / 2;
    console.log(middle_day);
});
Sign up to request clarification or add additional context in comments.

7 Comments

Hey, this is way neater.
I have to use result2 in $.each
@ImHappy - why? Do you bang in a nail with a slipper? Right tool for the right job (Or, is this homework?)
@ImHappy so what do you need the sum of all the numbers in result2? If not why cant you just iterate over the resultant array. This answer does exactly what you asked for in your 2 bullet points in the question - but just in a much neater way than you're trying to do it!
Now youve lost me - Thats exactly the result I get
|
0

Special case the first entry, like this :

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 = 0;
        if(i==0) 
        {
                  //this is the first entry, so we can't look back to the previous day.   
                  sum_day = obj.day; 
        }
        else
        {
                   //you were calling obj[i-1] here, which wasn't going to work.
                   //i presume you wanted the previous day here.
                   sum_day = checkin_info[i - 1].day + obj.day;
        }


        var middle_day = sum_day / 2;
        console.log(middle_day);
    });

8 Comments

any other way do in $.each ?
just noticed that and changed the answer.
$.each behaves different from normal loops. if you return a true, it will stop processing the current iteration and move on to the next.
if I change to checkin_info[i - 1].day first item will be skip. I dont want the first item to be skip . help
@ImHappy fixed it. good eye. Also, how can we calculate the middle of the day if we don't skip the first item? that leaves no reference to the day before, so we can't make your calculation if we don't skip the first one.
|
0

$.each is not good for performance as every interation would call function. It is better to use the for loop in vanilla js.

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,
        len = checkin_info.length,
        i,
        sum_day = [],
        middle_day = [],
        sum = 0;

    for(i=0; i< (len -1); i++) {
        sum = checkin_info[i].day + checkin_info[i + 1].day;
        sum_day.push(sum);
        middle_day.push(sum/2);
    }
    console.log(sum_day)
    console.log(middle_day)

2 Comments

well, it works. but the reasoning behind it is a little inane.
Look, we get that it's a little bit slower to use jquery's each. it's just so little overhead that you barely notice it on a data set like this.

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.