object-delete-key2.1.0

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.

Quick Take

import { strict as assert } from "assert";
import { deleteKey } from "object-delete-key";

// deleting key 'c', with value 'd'
assert.deepEqual(
  deleteKey(
    {
      a: "b",
      c: "d",
    },
    {
      key: "c",
      val: "d",
    }
  ),
  { a: "b" }
);

// deleting key 'b' with value - array ['c', 'd']
assert.deepEqual(
  deleteKey(
    {
      a: { e: [{ b: ["c", "d"] }] },
      b: ["c", "d"],
    },
    {
      key: "b",
      val: ["c", "d"],
    }
  ),
  {}
);
// notice program cleaned after itself, it didn't leave empty "a" key

Examples

Deleting

Three modes:

  • Delete all key/value pairs found in any nested plain objects where key equals value.
  • Delete all key/value pairs found in any nested plain objects where key is equal to a certain thing. value doesn't matter.
  • Delete all key/value pairs found in any nested plain objects where value is equal to a certain thing. key doesn't matter.

This library accepts anything as input, including parsed HTML, which is deeply nested arrays of plain objects, arrays and strings. You can feed anything as input into this library - if it's traversable, it will be traversed and searched for your key and/or value in any plain objects.

If you want to delete any nested objects that contain certain key/value pair(s), check out ast-delete-object.

API

deleteKey(input, options)

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

The input arguments are not mutated; this package clones them first before using.

API - Input

Input argument Type Obligatory? Description
input Whatever yes AST tree, or object or array or whatever. Can be deeply-nested.
options Object yes Options object. See its key arrangement below.
options object's key Type Obligatory? Default Description
key String no^ n/a Key to find and delete.
val Whatever no^ n/a Key's value to find and delete. Can be a massively nested AST tree or whatever.
cleanup Boolean no true Should this package delete any empty carcases of arrays/objects left after deletion?
only String no any Default setting will delete from both arrays and objects. If you want to delete from plain objects only, set this to one of "object-type" values below. If you want to delete keys from arrays only, set this to one of "array-type" values below. In this case "key" means array element's value and "value" is not meant to be used.

^ - at least one, key or val must be present.

Accepted opts.only values

Interpreted as "array-type" Interpreted as "object-type" Interpreted as "any" type
array object any
arrays objects all
arr obj everything
aray ob both
arr o either
a each

whatever

e

If opts.only is set to any string longer than zero characters and is not case-insensitively equal to one of the above, the object-delete-key will throw an error.

We want to relieve users from having to check the documentation for opts.only values.

API - Output

This library returns the input with all requested keys/value pairs removed.

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-fill-missing-keys 8.1.0
Add missing keys into plain objects, according to a reference object
📦 object-boolean-combinations 4.1.0
Consumes a defaults object with booleans, generates all possible variations of it
📦 object-no-new-keys 3.1.0
Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)
📦 object-flatten-referencing 5.1.0
Flatten complex nested objects according to a reference objects
📦 object-all-values-equal-to 2.1.0
Does the AST/nested-plain-object/array/whatever contain only one kind of value?
📦 object-merge-advanced 12.1.0
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-set-all-values-to 4.1.0
Recursively walk the input and set all found values in plain objects to something