object-flatten-all-arrays4.8.23

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.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 object-delete-key 1.9.38
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.8.25
Does the AST/nested-plain-object/array/whatever contain only one kind of value?
📦 object-boolean-combinations 2.11.66
Consumes a defaults object with booleans, generates all possible variations of it
📦 object-fill-missing-keys 7.10.28
Add missing keys into plain objects, according to a reference object
📦 object-set-all-values-to 3.9.67
Recursively walk the input and set all found values in plain objects to something
📦 object-flatten-referencing 4.11.27
Flatten complex nested objects according to a reference objects
📦 object-merge-advanced 10.11.29
Recursive, deep merge of anything (objects, arrays, strings or nested thereof), which weighs contents by type hierarchy to ensure the maximum content is retained