Flattie Object Flattening Utility

1.1.1 · active · verified Sun Apr 19

Flattie is a minimalist and highly performant JavaScript utility for recursively flattening nested objects and arrays into a single-depth object. Currently at version 1.1.1, the library maintains a stable release cadence with incremental patches. Its core differentiators include its extremely small bundle size (203B), demonstrated speed via benchmarks against alternatives like `flat` and `flatten-object`, and customizable behavior for key concatenation via a 'glue' string. By default, it automatically omits nullish values (`null`, `undefined`) from the flattened output, a feature that can be toggled. It also ships with TypeScript type definitions, providing a robust developer experience. Its counterpart, `nestie`, provides the reverse operation (expanding flattened objects).

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic object flattening, custom glue usage, and how to retain nullish values.

import { flattie } from 'flattie';

const nestedObject = {
  user: 'alice',
  profile: {
    email: 'alice@example.com',
    settings: {
      theme: 'dark',
      notifications: true,
      preferences: [
        { id: 1, value: 'optionA' },
        { id: 2, value: null },
        { id: 3, value: 'optionC' }
      ]
    }
  },
  status: null,
  tags: ['admin', 'premium']
};

// Flatten the object with default glue ('.') and nullish purging (true)
const flatDefault = flattie(nestedObject);
console.log('Default Flattened:', flatDefault);

// Flatten the object keeping nullish values
const flatKeepNullish = flattie(nestedObject, '.', true);
console.log('Flattened (keeping nullish):', flatKeepNullish);

// Flatten the object with a custom glue
const flatCustomGlue = flattie(nestedObject, '__');
console.log('Flattened (custom glue):', flatCustomGlue);

view raw JSON →