object-set-all-values-to3.9.67

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";

assert.deepEqual(
  setAllValuesTo({
    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:
assert.deepEqual(
  setAllValuesTo(
    {
      a: "a",
      b: "b",
      c: "c",
      d: "d",
    },
    "x"
  ),
  {
    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.

§ API

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).

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 object-no-new-keys 2.9.11
Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)
📦 object-flatten-referencing 4.11.27
Flatten complex nested objects according to a reference objects
📦 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-boolean-combinations 2.11.66
Consumes a defaults object with booleans, generates all possible variations of it
📦 object-all-values-equal-to 1.8.25
Does the AST/nested-plain-object/array/whatever contain only one kind of value?
📦 object-flatten-all-arrays 4.8.23
Merge and flatten any arrays found in all values within plain objects
📦 object-fill-missing-keys 7.10.28
Add missing keys into plain objects, according to a reference object