Know last index of a . each

Asked

Viewed 1,287 times

1

I’m walking a .each and I would like you to present in the last position a alert. So I’m trying to get the last position of index:

if (index == len - 1) {
    alert("Última posição");
}

Complete code:

$.getJSON("/Contrato/CriarCopiarContrato", { agrupamento: $("#listaAgrupamentos").val() },
   function (result) {
      $.each(result, function (index, itemData) {
          if (index == len - 1) {
              alert("ultima");
          }
      });
});

2 answers

5


Just you recover the size - length of your result. The code is shown below:

$.getJSON("/Contrato/CriarCopiarContrato", { agrupamento: $("#listaAgrupamentos").val() },
   function (result) {
      var len = result.length;
      $.each(result, function (index, itemData) {
          if (index == len - 1) {
              alert("ultima");
          }
      });
});
  • Had this line commented... Thanks for the repair :)

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.