0

i would like to display only the last item of my array. Here is my code :

script.js:

var occurrences = {};

for (i = 0; i < data.tickets.length; i++) {


        var tag = data.tickets[i].tags[0];
        console.log(tag);

        if (occurrences.hasOwnProperty(tag)) {
                   occurrences[tag]++;
               } else {
                   occurrences[tag] = 1;
               }


               console.log(occurrences["assistance"]);

        switch(status){
          case "open":
          break;
          case "closed":
          break;
          case "pending":
          break;
          case "solved":

          break;
        }

        $("#occurence").append('<p>'+occurrences["assistance"]+'</p>')

But this is what i get when i display occurences["assistance"]: I have the list of number and i just want to have the last one (in my case it is the number 22)

enter image description here

and console.log(data.tickets.length) give me that : enter image description here

xenurs
  • 459
  • 1
  • 8
  • 19
  • how does `data.tickets.length` looks? – guradio Apr 07 '16 at 07:47
  • Seems like you only want to work on last item of `data.tickets`. Why not get that in value and perform rest of the function - `item_in_question = data.tickets.(data.tickets.length - 1)`. – Bikas Apr 07 '16 at 07:50
  • Guys i want the last item of occurences["assistance"] ! this is not the same thing you should read my question until the last line :) – xenurs Apr 07 '16 at 07:54
  • 1
    if you're trying to append the final value of `occurrences["assistance"]` keep the append/console statement outside the for loop – Easwar Raju Apr 07 '16 at 07:54
  • Surely this is a duplicate of http://stackoverflow.com/questions/3216013/get-the-last-item-in-an-array?rq=1 – Alex Logan Apr 07 '16 at 07:55

4 Answers4

1

Please check below code

var full_array = /* some array here */;
var full_last_element = full_array[full_array.length - 1];
Dhaval Patel
  • 1,076
  • 6
  • 19
1

this is for last element

  var last = someArray[someArray.length-1]
Asad
  • 3,070
  • 7
  • 23
  • 61
0

You don't need to loop in an array to find the last item.

Try this:

var lastTicket = data.tickets[data.tickets.length - 1];

It will return the last item.

Huso
  • 1,501
  • 9
  • 14
-2

Ok i manage this i just need to put occurences["assistance"] after the for loop !

xenurs
  • 459
  • 1
  • 8
  • 19