Recursively walk the input and set all found values in plain objects to something

§ Quick Take

import { strict as assert } from "assert";
import { setAllValuesTo } from "object-set-all-values-to";

    a: "a",
    b: "b",
    c: "c",
    d: "d",
    a: false,
    b: false,
    c: false,
    d: false,

// you can change the default "false" to something else:
      a: "a",
      b: "b",
      c: "c",
      d: "d",
    a: "x",
    b: "x",
    c: "x",
    d: "x",

§ Purpose

Take any input: nested array, nested plain object or whatever really, no matter how deeply nested. Walk through it recursively and if you find any plain objects, assign all their keys to a given second input's argument OR default, false.

It does not mutate the input arguments. Operations are done on a cloned input.

We needed this library to overwrite all values to be false on JSON schema objects, so that later when we copy from key/value pairs from schema, values are equal to false and we don't need to prep them further.

This library is well-tested and is being used in commercial projects.


setAllValuesTo(input, value)

In other words, it's a function which takes two input arguments, both obligatory.

§ API - Input

Input argumentTypeObligatory?DefaultDescription
inputWhateveryesundefinedAST tree, or object or array or whatever. Can be deeply-nested. Hopefully contains some plain objects.
valueWhatevernofalseAssign all the found plain object values to this

§ API - Output

Same thing that you gave in the first argument, except with values overwritten (where applicable).

§ 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-all-values-equal-to 2.0.14
Does the AST/nested-plain-object/array/whatever contain only one kind of value?
📦 object-delete-key 2.0.14
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-boolean-combinations 4.0.14
Consumes a defaults object with booleans, generates all possible variations of it
📦 object-fill-missing-keys 8.0.14
Add missing keys into plain objects, according to a reference object
📦 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-all-arrays 5.0.14
Merge and flatten any arrays found in all values within plain objects
📦 object-flatten-referencing 5.0.14
Flatten complex nested objects according to a reference objects