score:2

Accepted answer

If your list is extremely long and you don't want to loop it more than once, you can do this all in one call:

<!DOCTYPE html>
<html>

<head>
  <script data-require="d3@4.0.0" data-semver="4.0.0" src="https://d3js.org/d3.v4.min.js"></script>
</head>

<body>
  <script>
    var input = [{
        x: new Date("2014-01-01"),
        y: 1223
      }, {
        x: new Date("2015-06-01"),
        y: 12
      }, {
        x: new Date("2015-07-01"),
        y: 1025
      }, {
        x: new Date("2015-08-01"),
        y: 125
      }, {
        x: new Date("2017-01-01"),
        y: 143
      }],
      minDate = new Date("2015-01-01"),
      maxDate = new Date("2016-01-01");

    var minMax = d3.extent(input, function(d) {
      if (d.x <= minDate || d.x >= maxDate){
        return NaN;
      }
      return d.y;
    })
    console.log(minMax);
  </script>
</body>

</html>

score:2

You have the possibility to filter the elements from within your array to fit the range of dates you need:

inputArray.filter(d => d.x > "2015-01-01" && d.x < "2016-01-01")

Then you can map (transform) the filtered elements to transform them into their associated y value:

.map(d => d.y)

To finally get the min or max of the resulting array:

d3.min(..)

which gives:

var input = [
  {
    x: "2014-01-01",
    y: 1223 
  },
  {
    x: "2015-06-01",
    y: 12
  },
  {
    x: "2015-07-01",
    y: 1025
  },
  {
    x: "2015-08-01",
    y: 125
  },
  {
    x: "2017-01-01",
    y: 143
  }
 ]

var output = input
  .filter(d => d.x > "2015-01-01" && d.x < "2016-01-01")
  .map(d => d.y);

console.log("filter/map array: " + output);
console.log("min: " + d3.min(output));
console.log("max: " + d3.max(output));
<script src="https://d3js.org/d3.v5.min.js"></script>


Related Query