Skip to content

nest

Groups a flat array of data by one or more key accessors into nested {key, values} entries, one level per accessor. A row whose keys run out before the last level becomes a leaf at the depth where they stopped instead of leaving an empty level.

The hierarchy builder charts such as Tree use: like nestGroups, but a single accessor can be passed without wrapping it in an array, and rows whose keys run out before the last level are collapsed into leaves instead of leaving an empty level behind.

import {nest} from "@d3plus/data";

const data = [
  {region: "North", year: 2023, sales: 120},
  {region: "North", year: 2024, sales: 135},
  {region: "South", year: 2023, sales: 80},
];

nest(data, d => d.region);

Parameters

import {nest} from "@d3plus/data";
datarequiredArray.<DataPoint>

The flat data array to nest.

keysrequiredKeyAccessor | Array.<KeyAccessor>

One key accessor, or an array of them, one per nest level.