object-flatten-all-arrays5.0.14

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 argumentTypeObligatory?Description
inputWhateveryesAST tree, or object or array or whatever. Can be deeply-nested. Hopefully contains some plain objects.
optionsPlain objectnoSet the options in this object. See below for keys.

§ Options Object

options object's keyTypeObligatory?DefaultDescription
flattenArraysContainingStringsToBeEmptyBooleannofalseIf 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-boolean-combinations 4.0.14
Consumes a defaults object with booleans, generates all possible variations of it
📦 object-all-values-equal-to 2.0.14
Does the AST/nested-plain-object/array/whatever contain only one kind of value?
📦 object-no-new-keys 3.0.14
Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)
📦 object-flatten-referencing 5.0.14
Flatten complex nested objects according to a reference objects
📦 object-merge-advanced 12.0.11
Recursively, deeply merge of anything (objects, arrays, strings or nested thereof), which weighs contents by type hierarchy to ensure the maximum content is retained
📦 object-set-all-values-to 4.0.14
Recursively walk the input and set all found values in plain objects to something
📦 object-fill-missing-keys 8.0.14
Add missing keys into plain objects, according to a reference object