object-flatten-all-arrays4.9.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 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 Sourcehut.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 object-boolean-combinations 3.0.0
Consumes a defaults object with booleans, generates all possible variations of it
📦 object-no-new-keys 2.10.0
Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)
📦 object-fill-missing-keys 7.11.1
Add missing keys into plain objects, according to a reference object
📦 object-delete-key 1.10.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-all-values-equal-to 1.9.1
Does the AST/nested-plain-object/array/whatever contain only one kind of value?
📦 object-merge-advanced 10.12.1
Recursive, deep merge of anything (objects, arrays, strings or nested thereof), which weighs contents by type hierarchy to ensure the maximum content is retained
📦 object-flatten-referencing 4.12.1
Flatten complex nested objects according to a reference objects