object-set-all-values-to5.0.1

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 argument Type Obligatory? Default Description
input Whatever yes undefined AST tree, or object or array or whatever. Can be deeply-nested. Hopefully contains some plain objects.
value Whatever no false Assign 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 3.0.1
Does the AST/nested-plain-object/array/whatever contain only one kind of value?
📦 object-flatten-referencing 6.0.1
Flatten complex nested objects according to a reference objects
📦 object-merge-advanced 13.0.1
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-flatten-all-arrays 6.0.1
Merge and flatten any arrays found in all values within plain objects
📦 object-fill-missing-keys 9.0.1
Add missing keys into plain objects, according to a reference object
📦 object-delete-key 3.0.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-boolean-combinations 5.0.1
Consumes a defaults object with booleans, generates all possible variations of it