title: Javascript: How to loop through an array of objects to get a property which decreases in value by n with each iteration based on the last result?
body: <p>What I would like is for value obtained in the condition in the loop to be used in the condition in the next iteration:</p>
  <pre><code>function MakeRoute(highestEl, n) {
    let startPointsForClosest = [];
  
    startPointsForClosest[0] = highestEl;
  
    for (let i = 0; i &lt; topographyData.length - 1; i++) {
      if (
        startPointsForClosest[i]?.topography.elevation &gt;=
        topographyData[i]?.topography.elevation - n
      ) {
        startPointsForClosest.push(topographyData[i]);
      }
    }
  
    return startPointsForClosest;
  }
  
  
  MakeRoute({
  &quot;latlng&quot;: {
      &quot;lat&quot;: 40.738800829681864,
      &quot;lng&quot;: -73.7568126408477
  },
  
  &quot;topography&quot;: {
      &quot;elevation&quot;: 38.600000000000364,
      &quot;slope&quot;: 1.3792065335718062,
      &quot;aspect&quot;: 111.03751102530578,
      &quot;resolution&quot;: 57.85092357410129
  }}, n)
  </code></pre>
  <p>Of course on the first iteration I start the value like:</p>
  <pre><code>   startPointsForClosest[0] = highestEl;
  </code></pre>
  <p>However this is what I get back</p>
  <p>This is what I got back.</p>
  <pre><code>[
      {
          &quot;latlng&quot;: {
              &quot;lat&quot;: 40.738800829681864,
              &quot;lng&quot;: -73.7568126408477
          },
          &quot;topography&quot;: {
              &quot;elevation&quot;: 38.600000000000364,
              &quot;slope&quot;: 1.3792065335718062,
              &quot;aspect&quot;: 111.03751102530578,
              &quot;resolution&quot;: 57.85092357410129
          }
      },
      {
          &quot;latlng&quot;: {
              &quot;lat&quot;: 40.741588319997106,
              &quot;lng&quot;: -73.76029372856034
          },
         
          &quot;topography&quot;: {
              &quot;elevation&quot;: 30.700000000000728,
              &quot;slope&quot;: 1.9044574779242354,
              &quot;aspect&quot;: 188.9726266146405,
              &quot;resolution&quot;: 57.84851723771878
          }
      }
  ]
  </code></pre>
  <p>Not sure why the function stops at two objects. But I should be getting at least an object with a elevation property of <code>36.600000000000364</code>?</p>
  <p>So what i'd want is to get an array which has a collection of objects which have a decrease in elevation by n. e.g. if n was <code>2</code>:</p>
  <pre><code>[
  {
      &quot;latlng&quot;: { },
      &quot;topography&quot;: {
          &quot;elevation&quot;: 38.600000000000364,
          
      }
  },
  {
      &quot;latlng&quot;: { },
      &quot;topography&quot;: {
          &quot;elevation&quot;: 36.600000000000364,
      }
  },
  {
      &quot;latlng&quot;: { },
      &quot;topography&quot;: {
          &quot;elevation&quot;: 34,
      }
  }
  </code></pre>
  <p>]</p>
  <p>Any help would be appreciated.</p>
  <p>Update: Here is an <a href="https://codepen.io/antonioOrtiz/pen/gOeEabp?editors=1112" rel="nofollow noreferrer">example</a>.</p>