moskitos
moskitos

Reputation: 149

count how many data in array and increment javascript

I'm new in stackoverflow: Here is my issue, i would like to count how many tickets by month a user has and push it in my array, i did that:

for(j=0; j< data.data.tickets.length ;j++){
   var requesterid = data.data.tickets[j].requester_id; 
   var created_at = data.data.tickets[j].created_at; 
   var today = new Date().toISOString().slice(0, 7);
    if(created_at.includes(today)&& requesterid == cleartab[requesterid]['id']){total ++}     

      var arrayRef2 = cleartab[requesterid]['monthly'] || [];  
      cleartab[requesterid]['monthly'] = arrayRef2.concat([{"janvier":total}], [{"fevier":"fef"}]); 
}

The problem is that it gave me wrong result.

Here is my array:

My array

If my question is not clear, i can re-explain or tell me if you need something more to answer it

I hope you can help me
My issue:
Some people should not have ticket the result is not the good one. I would like to be sure that it increment only one people when 1 ticket has been sent in the current month. For now, when someone send a ticket in the current month, every user got +1 ticket in the current month. But what i want is that: it increment only for one user, the user who sent the ticket. Is that clear ?

Upvotes: 0

Views: 73

Answers (1)

Dhananjaya Kuppu
Dhananjaya Kuppu

Reputation: 1322

Based on my understanding of the problem, you could try as below:

for(j=0; j< data.data.tickets.length ;j++){
    var requesterid = data.data.tickets[j].requester_id; 
    var created_at = data.data.tickets[j].created_at; 
    var today = new Date().toISOString().slice(0, 7);
    // read the monthly for a given requestor or
    // initialize the new array by setting the total
    // to 0 "janvier:0
    var arrayRef2 = cleartab[requesterid]['monthly'] || 
                    [{"janvier":0}, {"fevier":"fef"}]; 

    if(created_at.includes(today) && 
       requesterid == cleartab[requesterid]['id']){
       // increment the total, very first time the value of
       //  arrayRef2[0].janvier will be zero, but in
       //  next iteration it will be always the previous value
       arrayRef2[0].janvier++;
     }     

     cleartab[requesterid]['monthly'] = arrayRef2; 
 }

Upvotes: 1

Related Questions