简体   繁体   中英

Display the last item of an array

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)

在此处输入图片说明

and console.log(data.tickets.length) give me that : 在此处输入图片说明

Please check below code

var full_array = /* some array here */;
var full_last_element = full_array[full_array.length - 1];

这是最后一个元素

  var last = someArray[someArray.length-1]

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.

好的,我可以解决这个问题,我只需要将forences [“ assistance”]放在for循环之后!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM