d3-array is a comprehensive JavaScript library for array manipulation, statistical analysis, searching, transformations, and mathematical operations on iterables. It provides essential data processing utilities commonly used in data visualization and analysis applications, particularly as part of the D3.js ecosystem.
npm install d3-arrayimport { min, max, extent, mean, median } from "d3-array";For CommonJS:
const { min, max, extent, mean, median } = require("d3-array");For vanilla HTML with ES modules:
<script type="module">
import { min, max, extent } from "https://cdn.jsdelivr.net/npm/d3-array@3/+esm";
</script>For legacy environments with UMD:
<script src="https://cdn.jsdelivr.net/npm/d3-array@3"></script>
<script>
const { min, max, extent } = d3;
</script>import { min, max, extent, mean, group, ascending } from "d3-array";
// Basic statistics
const numbers = [3, 1, 4, 1, 5, 9, 2, 6];
const minimum = min(numbers); // 1
const maximum = max(numbers); // 9
const range = extent(numbers); // [1, 9]
const average = mean(numbers); // 3.875
// Working with objects using accessors
const data = [
{ name: "Alice", score: 95 },
{ name: "Bob", score: 87 },
{ name: "Charlie", score: 92 }
];
const maxScore = max(data, d => d.score); // 95
const minScore = min(data, d => d.score); // 87
// Grouping data
const grouped = group(data, d => d.score > 90 ? "high" : "low");
// Map { "high" => [{name: "Alice", score: 95}, {name: "Charlie", score: 92}], "low" => [{name: "Bob", score: 87}] }
// Sorting
const sorted = data.sort((a, b) => ascending(a.score, b.score));d3-array is built around several key design principles:
Core statistical functions for computing summary statistics, distributions, and mathematical operations on arrays of numbers.
function min(iterable, accessor?): any;
function max(iterable, accessor?): any;
function extent(iterable, accessor?): [any, any];
function mean(iterable, accessor?): number | undefined;
function median(iterable, accessor?): number | undefined;
function sum(iterable, accessor?): number;
function variance(iterable, accessor?): number | undefined;
function deviation(iterable, accessor?): number | undefined;Functions for finding elements, binary search operations, and comparisons within sorted and unsorted arrays.
function least(iterable, comparator?): any;
function greatest(iterable, comparator?): any;
function bisectLeft(array, x, lo?, hi?): number;
function bisectRight(array, x, lo?, hi?): number;
function ascending(a, b): number;
function descending(a, b): number;Functions for grouping, sorting, filtering, and reshaping data structures with powerful aggregation capabilities.
function group(iterable, ...keys): InternMap;
function rollup(iterable, reduce, ...keys): InternMap;
function cross(...iterables, reducer?): any[];
function merge(iterables): any[];
function transpose(matrix): any[][];Universal functions that work with any iterable, providing array-like operations for Sets, Maps, and Generators.
function every(iterable, test): boolean;
function some(iterable, test): boolean;
function filter(iterable, test): any[];
function map(iterable, mapper): any[];
function sort(iterable, comparator?): any[];Set theory operations for computing unions, intersections, differences, and relationships between iterables.
function union(...iterables): InternSet;
function intersection(...iterables): InternSet;
function difference(iterable, ...others): InternSet;
function superset(a, b): boolean;
function subset(a, b): boolean;Histogram generation and data binning functionality for creating frequency distributions and visualizations.
function bin(): BinGenerator;
function thresholdSturges(values): number;
function thresholdFreedmanDiaconis(values, min, max): number;
function thresholdScott(values, min, max): number;Functions for generating ranges, tick values, and other numeric sequences commonly used in scales and axes.
function range(start?, stop, step?): number[];
function ticks(start, stop, count): number[];
function tickStep(start, stop, count): number;
function nice(start, stop, count): [number, number];// Core types from internmap dependency
class InternMap<K, V> extends Map<K, V> {
constructor(entries?, key?);
}
class InternSet<T> extends Set<T> {
constructor(values?, key?);
}
// Bin generator and result types
interface BinGenerator {
(data: Iterable<any>): Bin[];
value(value?: (d: any, i: number, data: any[]) => number): BinGenerator;
domain(domain?: [number, number] | ((values: number[]) => [number, number])): BinGenerator;
thresholds(thresholds?: number | number[] | ((values: number[], min: number, max: number) => number | number[])): BinGenerator;
}
interface Bin extends Array<any> {
x0: number;
x1: number;
}
// Adder for high-precision arithmetic
class Adder {
constructor();
add(value: number): Adder;
valueOf(): number;
}