title: Compare n with n+1 in nested array and check if n+1 is within range of 500 from n
body: <p>this is my first question on StackOverflow so I might not include some important info. I need your help with understanding how to reflect the following task in code. </p>
  
  <p>I am setting up a chart and I need to display price points on the chart. The issue is that I do not want to show all the prices on the chart. I want to show only a price if it is outside of range from the previous element of the nested array. </p>
  
  <p>So what I need to do in steps is: </p>
  
  <ol>
  <li>Set up a range around elements of an arry. The range should be -500 [n] +500.  </li>
  <li>Compare values from an array. Compare n with n+1, n+2, n+3 etc. to see if n+2, n+3 n+4 etc is within range of 500 units from n. </li>
  <li>If it is within range, do not add that value to the array. If it is outside of the range 500, then add that value to the final array AND update the price item by moving the RANGE METER now to the next element.Then repeat the loop. </li>
  </ol>
  
  <p>The data that I am passing to the chart is: </p>
  
  <pre class="lang-js prettyprint-override"><code>var dataForChart = [ 
    { "id": 1111, "data": [
      {"x": "11.04.2019", "y": 25000}, 
      {"x": "11.05.2019", "y": 25200}, 
      {"x": "15.06.2019", "y": 28000}, 
      {"x": "19.07.2019", "y": 30000} ] 
    } , 
    { "id": 2222, "data": [
      {"x": "11.04.2019", "y": 1}, 
      {"x": "11.05.2019", "y": 501}, 
      {"x": "15.06.2019", "y": 550}, 
      {"x": "19.07.2019", "y": 2000} ] 
    }, 
    { "id": 3333, "data": [
      {"x": "11.04.2019", "y": 1000}, 
      {"x": "11.05.2019", "y": 1200}, 
      {"x": "15.06.2019", "y": 1600}, 
      {"x": "19.07.2019", "y": 1700} ] 
    } ] 
  </code></pre>
  
  <p>So to use the above data. </p>
  
  <p>If dataForCharts[0].data[0].y is within range 500 from dataForCharts[0].data[1].y, go on with the loop. Once you find dataForCharts[0].data[2].y which is 28000, so outside of range 500 from 25000, then add that price to the final array(to be displayed on the chart), AND now move the RANGE METER to 28000 price point. From now on, count 500 from 28000. So dataForCharts[0].data[3].y = 30000 would be also pushed to the final array. </p>
  
  <p>Move on to the next ID so dataForCharts[1].data[0].y and set the RANGE METER to the dataForCharts[1].data[0].y so start from 1 and not from 28000. </p>
  
  <p>The expected result should be filtered nested arrays of "data": </p>
  
  <pre class="lang-js prettyprint-override"><code>var dataForChart = [ 
    { "id": 1111, "data": [
      {"x": 11.04.2019, "y": 25000}, 
      {"x": 15.06.2019, "y": 28000}, 
      {"x": 19.07.2019, "y": 30000} ] 
    } , 
    { "id": 2222, "data": [
      {"x": 11.04.2019, "y": 1}, 
      {"x": 15.06.2019, "y": 550}, 
      {"x": 19.07.2019, "y": 2000} ] 
    }, 
    { "id": 3333, "data": [
      {"x": 11.04.2019, "y": 1000}, 
      {"x": 15.06.2019, "y": 1600}] 
    } ]
  </code></pre>
  
  <p>What I thought of is to create a function for the RANGE METER that will check if -500 &lt; n &lt; +500. I think I will will also need to push values to the array. To access the nested array I would use map, which creates a new array. I was trying to implement _.inRange for this task but I am not able to incorporate it in a meaningful manner for this task. Your help would be much appreciated. </p>
  
  <p>I hope I explained what I am trying to achive clearly. Let me know if you need more explanation.</p>