Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→object-delete-keynext

object-delete-key4.2.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.

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • DELETING
  • API — DELETEKEY()
  • API — TYPES
  • Changelog

Installation

Quick Take

Examples

  • Deletion by the key only
  • Deletion by the value only
  • Disable the cleanup which would otherwise happen after deletion
  • Delete only when both the key and value match
  • Delete matching object keys without deleting matching array values
  • Wildcards

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

The main function deleteKey() is imported like this:

It’s a function which takes three input arguments:

Input argumentTypeObligatoryDescription
input
Type: Plain object
Obligatory: yes
inputPlain objectyesInput to work upon.
options
Type: Plain object
Obligatory: yes
optionsPlain objectyesObligatory Options object.

The Obligatory Options Object has the following shape:

KeyTypeObligatoryDefaultDescription
key
Type: String
Obligatory: no^
Default: n/a
keyStringno^n/aKey to find and delete.
val
Type: Whatever
Obligatory: no^
Default: n/a
valWhateverno^n/aKey’s value to find and delete.
cleanup
Type: Boolean
Obligatory: no
Default: true
cleanupBooleannotrueShould this package delete any empty carcases of arrays/objects left after the deletion?
only
Type: "array", "object" or "any"
Obligatory: no
Default: "any"
only"array", "object" or "any"no"any"Default setting will delete from both arrays and objects. Here you can limit the scope.

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

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

API — types

This package is written in TypeScript and exports the following types:

TypeDescription
Obj
Type: Obj
ObjA plain object with string keys and values of any type — what deleteKey() takes and returns.
Opts
Type: Opts
OptsThe Optional Options Object of deleteKey(), documented above.
import type { Obj, Opts } from "object-delete-key";

Changelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab