0

I am having troubles trying to check if the date exists in the array.

 for(var i = 0; i< crisislist.length; i++){
        hazecounter = 1;
        if(crisislist[i].category == 1){
            if(crisislist[i].date != crisislist[i+1].date) {
                hazelabel.push(crisislist[i].date);
            }else{
                hazecounter++; 
            }
            hazedata.push(hazecounter);
        }
 }

The sample data for the date is:

["01-02-2017", "22-03-2017", "22-03-2017", "07-08-2017"]

And the expected output for hazelabel, hazedata should be:

hazelabel: ["01-02-2017", "22-03-2017", "07-08-2017"] hazedata: [1,2,1]

With the code above, when I check until the last element in the array and trying to make a comparison, it throw me an error message:

Uncaught TypeError: Cannot read property 'date' of undefined

I think this is because when I reach the last element of array, and I try to find crisislist[I+1].date, it could not found and thus the error message.

Any idea how to fix this? Thanks in advance!

3
  • I don't get it , need more code . Commented Apr 1, 2017 at 17:53
  • Can't you just check if crisislist[i] + 1 is not undefined before accessing the date property? Commented Apr 1, 2017 at 17:54
  • you can write it simply like this stackoverflow.com/questions/40418507/…. jsfiddle demo jsfiddle.net/u6u80zsa Commented Apr 1, 2017 at 18:17

4 Answers 4

0

You must access crisislist[i+1].date only when i doesn't point to the last element.

Also notice that to get the desired result, you need to move the hazedata.push inside the if block and put the initialisation of hazecounter in front of the loop.

var hazecounter = 1;
for (var i = 0; i< crisislist.length; i++) {
    if (crisislist[i].category == 1) {
        if (i == crisislist.length-1 || crisislist[i].date != crisislist[i+1].date) {
            hazelabel.push(crisislist[i].date);
            hazedata.push(hazecounter);
            hazeCounter = 1;
        } else {
            hazecounter++; 
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it works! I was thinking how to check for the last element.
0

Your if statement is going to be a problem.

if(crisislist[i].date != crisislist[i+1].date) {

You are accessing crisislist[i+1] in a loop that goes to < crisislist.length. That means that if you have an array of size 4, your loop will go until i = 3, but you are accessing i+1 from the array (crisislist[4]), which will be undefined.

Try changing your for loop to go to crisis.length-1

Comments

0

You just need to check till second last :

for(var i = 0; i< (crisislist.length-1); i++){
        hazecounter = 1;
        if(crisislist[i].category == 1){
            if(crisislist[i].date != crisislist[i+1].date) {
                hazelabel.push(crisislist[i].date);
                if (crisislist.length-2 == i)
                {
                    hazelabel.push(crisislist[i+1].date);
                }
            }else{
                hazecounter++; 
            }
            hazedata.push(hazecounter);
        }
 }

3 Comments

But that would ignore the last one?
just try out it will be compared with second last so it will not
compared, yes, but the last item is not looked at separately.
0

Check that code. If you have any questions, add a comment :) In my solution dates dont have to be sorted.

  </head>

<BODY>
<script>

function Something(date)
{
    this.date = date;
    this.category = 1;
}

var crisislist = [];
var hazelabel = [];
var hazedata = [];
crisislist[0] = new Something("01-02-2017");
crisislist[1] = new Something("22-03-2017");
crisislist[2] = new Something("22-03-2017");
crisislist[3] = new Something("07-08-2017");




 for(var i = 0; i< crisislist.length; i++){
    if(crisislist[i].category == 1)
    {
     if(!hazelabel[crisislist[i].date])
     {
         hazelabel[crisislist[i].date] = crisislist[i].date;
         hazedata[crisislist[i].date] = 1;
     }
     else
     {
         hazedata[crisislist[i].date]++;
     }
    }
 }

for(var key  in hazelabel)
{
console.log(hazelabel[key]);
console.log(hazedata[key]);  
}

</script>
</BODY>
</HTML>

Comments

Your Answer

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