object-delete-key1.9.39

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 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
arrayobjectany
arraysobjectsall
arrobjeverything
arayobboth
arroeither
aeach

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.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 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-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-set-all-values-to 3.9.67
Recursively walk the input and set all found values in plain objects to something
📦 object-merge-advanced 10.11.30
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-fill-missing-keys 7.10.29
Add missing keys into plain objects, according to a reference object
📦 object-boolean-combinations 2.11.66
Consumes a defaults object with booleans, generates all possible variations of it