object-flatten-all-arrays6.0.1

Merge and flatten any arrays found in all values within plain objects

Quick Take

import { strict as assert } from "assert";
import { flattenAllArrays } from "object-flatten-all-arrays";

assert.deepEqual(
  flattenAllArrays({
    a: "a",
    b: "b",
    c: [
      {
        b: "b",
        a: "a",
      },
      {
        d: "d",
        c: "c",
      },
    ],
  }),
  {
    a: "a",
    b: "b",
    c: [
      {
        a: "a",
        b: "b",
        c: "c",
        d: "d",
      },
    ],
  }
);

Purpose

Recursively traverse the deeply-cloned input and merge all plain objects within each array.

API

flattenAllArrays(input, [options])

In other words, it's a function which takes two input arguments, second-one being optional (marked by square brackets).

Program returns the same type thing as given, only with arrays (recursively) flattened.

API - Input

None of the input arguments are mutated. Their clones are being used instead.

Input argument Type Obligatory? Description
input Whatever yes AST tree, or object or array or whatever. Can be deeply-nested. Hopefully contains some plain objects.
options Plain object no Set the options in this object. See below for keys.

Options Object

options object's key Type Obligatory? Default Description
flattenArraysContainingStringsToBeEmpty Boolean no false If any arrays contain strings, flatten them to be empty thing. This is turned off by default, but it's what you actually need most of the time.

Changelog

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

Contributing

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.

Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 object-set-all-values-to 5.0.1
Recursively walk the input and set all found values in plain objects to something
📦 object-no-new-keys 4.0.1
Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)
📦 object-delete-key 3.0.1
Delete keys from all arrays or plain objects, nested within anything, by key or by value or by both, and clean up afterwards. Accepts wildcards
📦 object-flatten-referencing 6.0.1
Flatten complex nested objects according to a reference objects
📦 object-boolean-combinations 5.0.1
Consumes a defaults object with booleans, generates all possible variations of it
📦 object-fill-missing-keys 9.0.1
Add missing keys into plain objects, according to a reference object
📦 object-all-values-equal-to 3.0.1
Does the AST/nested-plain-object/array/whatever contain only one kind of value?