Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find last item in a .each()?

I am running the following but I get

(index):58 Uncaught TypeError: jQuery.last is not a function

var myId; 
 jQuery.getJSON("https://en.wikipedia.org/w/api.php?action=query&list=embeddedin&eilimit=500&eititle=Template:Infobox&callback=?", {
    disablelimitreport: true,
    format: "json"
  }, function(data) {
    jQuery.each(data.query.embeddedin, function(i, item) {
    var $this = $(this);
    if($this[0] === jQuery.last()[0]) {
       var myId = item.pageid;
    }
    console.log(myId);
    });
  });

JsFidlle see console

like image 371
Roberto Marras Avatar asked Jan 28 '26 02:01

Roberto Marras


1 Answers

Just get the length of the element and move the index back.. Try this one. Hope will work in you.

var myId; 
jQuery.getJSON("https://en.wikipedia.org/w/api.php?
action=query&list=embeddedin&eilimit=500&eititle=Template:Infobox&callback=?", {
   disablelimitreport: true,
   format: "json"
}, function(data) {
  jQuery.each(data.query.embeddedin, function(index, item) { 
     var length = data.query.embeddedin.length;
     if (index === (length - 1)) {
       var myId = item.pageid;
       console.log('The last pageid is : ' + myId);
     } 
  });
});
like image 164
smzapp Avatar answered Jan 29 '26 16:01

smzapp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!