0

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 ?

6
  • What is "wrong" with the result? Commented Apr 29, 2016 at 11:56
  • 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 ? Commented Apr 29, 2016 at 11:57
  • Isn't that because you're using a global variable total which is shared by everyone? Have you tried making that a local variable? Commented Apr 29, 2016 at 12:07
  • Please don't post images of text (or JavaScript arrays in your case). Copy the text and post it as a code snippet. That way people can copy it and use it, and nothing is hidden as is the current case. Commented Apr 29, 2016 at 12:08
  • The variable total is something different what is interesting is my array "monthly" Commented Apr 29, 2016 at 12:15

1 Answer 1

1

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; 
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot ! I just want to understand what you did, can you explain to me please :)
I have written the comments, basically whatever you have done the same but the total value is now being always taken from and incremented from cleartab[requesterid]['monthly'] so it will always be different for different users.

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.