Public
Edited
Apr 14, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function getNextCombination(combination, n) {
if (!combination) return null;

// get index of next element to increment
const nextIndex = combination.reduceRight((a, v, i, arr) => arr[i+1] - v > 1 ? i : a, combination.length-1);

// guard against exceeding `n`
if (combination[nextIndex] > n) return null;

// increment elements into a new combination
return combination.map((v, i) => {
if (i < nextIndex) return i;
if (i === nextIndex) return v + 1;
if (i > nextIndex) return v;
});
}
Insert cell
Insert cell
function shuffleAndSort(arr, onKey) {
if (!onKey) {
return arr.sort((a, b) => a !== b ? a - b : Math.random() - 0.5);
}
return arr.sort((a, b) => a[onKey] !== b[onKey]
? a[onKey] - b[onKey]
: Math.random() - 0.5);
}
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