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'
      a: "b",
      c: "d",
      key: "c",
      val: "d",
  { a: "b" }

// deleting key 'b' with value - array ['c', 'd']
      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.


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 argumentTypeObligatory?Description
inputWhateveryesAST tree, or object or array or whatever. Can be deeply-nested.
optionsObjectyesOptions object. See its key arrangement below.
options object's keyTypeObligatory?DefaultDescription
keyStringno^n/aKey to find and delete.
valWhateverno^n/aKey's value to find and delete. Can be a massively nested AST tree or whatever.
cleanupBooleannotrueShould this package delete any empty carcases of arrays/objects left after deletion?
onlyStringnoanyDefault 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



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

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 object-fill-missing-keys 7.11.1
Add missing keys into plain objects, according to a reference object
📦 object-no-new-keys 2.10.0
Check, does a plain object (AST/JSON) has any unique keys, not present in a reference object (another AST/JSON)
📦 object-boolean-combinations 3.0.0
Consumes a defaults object with booleans, generates all possible variations of it
📦 object-flatten-all-arrays 4.9.1
Merge and flatten any arrays found in all values within plain objects
📦 object-flatten-referencing 4.12.1
Flatten complex nested objects according to a reference objects
📦 object-merge-advanced 10.12.1
Recursive, deep merge of anything (objects, arrays, strings or nested thereof), which weighs contents by type hierarchy to ensure the maximum content is retained
📦 object-all-values-equal-to 1.9.1
Does the AST/nested-plain-object/array/whatever contain only one kind of value?