title: Compare with the next element in foreach javascript
body: <p>In my project I have to check for availability of time of a timetable, in a way to insert a button that should contains two values as text. The day starts at 8 and ends at 18, each day has its sessions. I have a JSON array that contains all the sessions of the day, and each session has a start hour and an end hour, so when I find any space of time I want to append a button to my html. This button will have some text (example : 8->10) with an <code>onClick</code> method that will take me to another function. Here is some code that I hope it can make my problem more understandable:</p>
  
  <pre><code>function evenement(id)
  {
    $.ajax({
          url : "&lt;?php echo site_url('index.php/salle/evenement/')?&gt;/" + id,
          type: "GET",
          dataType: "JSON",
          success : function(data)
          {     var start_date =8;
                var end_date =18;
                var buttons ="";
              data.forEach(function(element, index, array)
              {
                  for (var i = start_date; i &lt; end_date; i++) {
                        if (element.date_debut == i) {i=element.date_fin;}
                        if (element.date_debut != i) {buttons +="&lt;button&gt;"+i+"-&gt;"+element[index+1].date_fin+"&lt;/button&gt;";}
                  }
  
              }
  
              );
               $('.modal-title').text('Disponibilité');
          $('#corps').html(buttons);
        $('#groupe_modal').modal('show');
  
          },
        });
  
  }
  </code></pre>
  
  <p>My problem is that I can't reach the next element while I'm working on the current one. The data is a parameter that I received in an ajax function and this code is just to make my self clear and I wrote it in a different section of my project. Thank you!</p>