Published
Edited
Sep 22, 2021
1 fork
Importers
28 stars
Insert cell
Insert cell
function samples(array, m) {
if (!((m = Math.floor(m)) > 0)) return []; // return nothing
const n = array.length;
if (!(n > m)) return [...array]; // return everything
if (m === 1) return [array[n >> 1]]; // return the midpoint
return Array.from({length: m}, (_, i) => array[Math.round(i / (m - 1) * (n - 1))]);
}
Insert cell
Insert cell
Insert cell
samples(array, m) // m evenly-spaced samples
Insert cell
array = Array.from({length: 1000}, (_, i) => i) // fake source data
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more