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!

  • I don't get it , need more code . – Ahmed Eid Apr 01 '17 at 17:53
  • Can't you just check if `crisislist[i] + 1` is not `undefined` before accessing the `date` property? – Teemoh Apr 01 '17 at 17:54
  • you can write it simply like this http://stackoverflow.com/questions/40418507/javascript-filter-array-with-duplicate-dates. jsfiddle demo https://jsfiddle.net/u6u80zsa/ – Sankara Apr 01 '17 at 18:17

4 Answers4

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);
        }
 }
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
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

jas7457
  • 1,712
  • 13
  • 21
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++; 
        }
    }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
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>
5ka
  • 246
  • 2
  • 6