<div id="root"></div>
// import some stuff from lodash
const { flow, map, sum, filter } = _;

// this is just here to display results to DOM
const addResultToDOM = (...args) =>
  $("#root").append(`<div>${args.join(" ")}</div>`);

const getAvgInView = series => {
  const data = flow(filter(point => point.isInside), map(point => point.y))(
    series.points
  );
  return (sum(data) / data.length).toFixed(1);
};

const s = {
  points: [
    { x: 0, y: 10, isInside: false },
    { x: 1, y: 100, isInside: false },
    { x: 2, y: 50, isInside: true },
    { x: 3, y: 10, isInside: true },
    { x: 4, y: 30, isInside: true },
    { x: 5, y: 10, isInside: false },
    { x: 6, y: 1, isInside: false },
  ]
};

addResultToDOM(`getAvgInView(s)`, getAvgInView(s));

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdn.jsdelivr.net/g/[email protected](lodash.min.js+lodash.fp.min.js)
  2. https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js