Turns an array of arrays of data into a nested tree of plain objects

Quick Take

import { strict as assert } from "assert";
import { generateAst } from "array-of-arrays-into-ast";

  generateAst([[1, 2, 3], [1, 2], [5]]),
    1: [
        2: [
            3: [null],
    5: [null],


It consumes array of arrays and produces a trie opens in a new tab-like AST from them. This library was a piece of a one experimental code generator of ours.



API - Input

Input argument Type Obligatory? Description
input Array of zero or more arrays yes Source of data to put into an AST
otps Plain object no An Optional Options Object. See its API below.

An Optional Options Object

Type: object - an Optional Options Object.

options object's key Type Default Description
dedupe Boolean true Skip duplicates

Here are all defaults in one place for copying:

dedupe: true,

When unused, Optional Options Object can also be passed as a null or undefined value.

API - Output

Type Description
Plain object AST of the input


If you generate the AST with default settings, dedupe setting will be active and duplicate paths won't be created:

import generateAst from "array-of-arrays-into-ast";
const res = generateAst([[1], [1], [1]]);
`${`\u001b[${33}m${`res`}\u001b[${39}m`} = ${JSON.stringify(res, null, 4)}`
// res = {
// 1: [null]
// }

Now, see what happens when you turn off opts.dedupe:

import generateAst from "array-of-arrays-into-ast";
const res = generateAst([[1], [1], [1]], { dedupe: false });
`${`\u001b[${33}m${`res`}\u001b[${39}m`} = ${JSON.stringify(res, null, 4)}`
// res = {
// 1: [null, null, null]
// }

Notice how entries for each branch were created.

Generally, we don't see the reason why you'd want duplicates, but the setting is there if you ever need it. 👍🏻


Every object's key will have a value of array.

  • null inside that array means it's the tip of the branch.

  • An object inside that array means the branch continues.


Compared vs. datastructures-js

There are libraries that produce and manage trie data structures, for example, datastructures-js opens in a new tab. In particular case, the problem is, the data structure is abstracted behind the let trie = ds.trie(); and you can't access it directly, traversing the nested tree of arrays and objects.

datastructures-js opens in a new tab trie would limit to search(), traverse() and count() methods. However, we need to recursively traverse every node and look up and down, what's around it.

Here's where this library comes in. It doesn't abstract the data it's producing - you get a nested plain object which you can traverse and further process any way you like, using a vast ocean of object- processing libraries.


See it in the monorepo opens in a new tab, on GitHub.


To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.


MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 array-group-str-omit-num-char 5.0.1
Groups array of strings by omitting number characters
📦 arrayiffy-if-string 4.0.1
Put non-empty strings into arrays, turn empty-ones into empty arrays. Bypass everything else
📦 array-of-arrays-sort-by-col 4.0.1
Sort array of arrays by column, rippling the sorting outwards from that column
📦 array-includes-with-glob 4.0.1
Like _.includes but with wildcards
📦 array-pull-all-with-glob 6.0.1
Like _.pullAll but with globs (wildcards)