1

My variable tag returns one of these 4 different values: assistance, bug, evolution or maintenance. I would like to count how many times I have each of those words. I would like to display how many times I have each of those item in my console first. I really don't know how to do that. Here is my code :

function displaytickets(y){
    $.ajax({
        url: "https://cubber.zendesk.com/api/v2/users/" + y + "/tickets/requested.json?per_page=150",
        type: 'GET',
        dataType: 'json',
        cors: true ,
        contentType: 'application/json',
        secure: true,
        beforeSend: function(xhr) {
            xhr.setRequestHeader ("Authorization", "Basic " + btoa(""));
        },
        success: function(data) {
            var sortbydate = data.tickets.sort(function(a, b){ 
                return new Date(b.created_at) - new Date(a.created_at); 
            });
            var ids = $.map(data.tickets, function (data) { 
                return data.id;
            })
            localStorage.setItem("mesid", ids);

            for (i = 0; i < data.tickets.length; i++) {
                var myticket = data.tickets[i];
                var mydate = data.tickets[i].created_at;
                var created = moment(mydate).format("MM-DD-YY");
                var mytitle = data.tickets[i].subject;
                var description = data.tickets[i].description;
                var status = data.tickets[i].status;
                var tag = data.tickets[i].tags[0];
                console.log(tag);
                var myid = data.tickets[i].id;
            }

            var nbticket = data.tickets.length;
            $("#name").append('<h2 class="title">' + " " + nbticket + " ticket(s)" + '</h2>');
        },
    });
}

Here's what I get from the console for the console.log(tag):

enter image description here

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
xenurs
  • 459
  • 1
  • 8
  • 19
  • You forgot to close a string: `"Basic " + btoa("));` (which is why the rest of your code is red) – somethinghere Apr 06 '16 at 15:10
  • no i don't forgot i delete it because there was my username and password to connect to my API – xenurs Apr 06 '16 at 15:12
  • 3
    Indeed, but it makes a difference. I didn't say that was the problem :) – somethinghere Apr 06 '16 at 15:13
  • Possible duplicate of [How to count string occurrence in string?](http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string) – Derek Pollard Apr 06 '16 at 15:18
  • @GuillaumeNouhaud you could use indexOf to know if it is present in a loop with the start paramater set to the next character of the last one found until the result is lower than 0. – Dominique Fortin Apr 06 '16 at 15:23

3 Answers3

1

Did you try to count it in your for loop ?

var maintenance_counter = 0;
 for (i = 0; i < data.tickets.length; i++) {
    var myticket = data.tickets[i];
    var mydate = data.tickets[i].created_at;
    var created = moment(mydate).format("MM-DD-YY");
    var mytitle = data.tickets[i].subject;
    var description = data.tickets[i].description;
    var status = data.tickets[i].status;
    var tag = data.tickets[i].tags[0];
    var myid = data.tickets[i].id;

    if( tag == "maintenance" ){
        maintenance_counter++;
    }

}

alert("Total maintenance occurrence:"+ maintenance_counter);
hmd
  • 960
  • 2
  • 9
  • 13
1

You can achieve this by using an object to store the occurrence count, keyed by the string itself. Try this:

var occurrences = {};

Then in your success handler you can add and increment the tags as you find them:

success: function(data) {
    // your code here...

    for (i = 0; i < data.tickets.length; i++) {
        // your code here...

        var tag = data.tickets[i].tags[0];
        if (occurrences.hasOwnProperty(tag)) {
            occurrences[tag]++;
        } else {
            occurrences[tag] = 1;
        }            
    }

    console.log(occurrences);
},

Working example

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

Create an object to hold your tag result count, similar to this:

var tagCount = {};
for (i = 0; i < data.tickets.length; i++) {
  var tag = data.tickets[i].tags[0];
  if (tagCount[tag] === undefined) {
    tagCount[tag] = 1;
  } else {
    tagCount[tag] += 1;
  } 

}

console.log(tagCount);
NOBrien
  • 459
  • 3
  • 7