CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-unset

The lodash method _.unset exported as a module for removing object properties at specified paths.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

lodash.unset

The lodash method _.unset exported as a standalone Node.js module for removing object properties at specified paths. This package provides a focused, dependency-free way to safely remove nested properties from objects using flexible path notation.

Package Information

  • Package Name: lodash.unset
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.unset

Core Imports

var unset = require('lodash.unset');

Note: This package is built for CommonJS and does not provide native ES6 module exports.

Basic Usage

var unset = require('lodash.unset');

// Create a test object
var object = { 'a': [{ 'b': { 'c': 7 } }] };

// Remove nested property using dot notation
var result = unset(object, 'a[0].b.c');
console.log(result);  // => true
console.log(object);  // => { 'a': [{ 'b': {} }] }

// Remove property using array path
var object2 = { 'a': [{ 'b': { 'c': 7 } }] };
var result2 = unset(object2, ['a', '0', 'b', 'c']);
console.log(result2); // => true
console.log(object2); // => { 'a': [{ 'b': {} }] }

Capabilities

Property Removal

Removes the property at the specified path of an object. The method mutates the original object and supports various path formats.

/**
 * Removes the property at `path` of `object`.
 * 
 * Note: This method mutates `object`.
 * 
 * @param {Object} object The object to modify.
 * @param {Array|string} path The path of the property to unset.
 * @returns {boolean} Returns `true` if the property is deleted, else `false`.
 */
function unset(object, path);

Parameters:

  • object (Object): The object to modify. Can be null or undefined (treated as no-op).
  • path (Array|string): The path of the property to unset. Supports multiple formats:
    • Dot notation: 'a.b.c'
    • Bracket notation: 'a[0].b.c', 'a["key"].b'
    • Mixed notation: 'a[0].b["key"].c'
    • Array path: ['a', 0, 'b', 'key', 'c']

Returns:

  • boolean: Returns true if the property is deleted, false if the property doesn't exist or deletion fails.

Behavior:

  • Mutates original object: The function modifies the input object directly.
  • Safe with null/undefined: Returns true for null or undefined objects (no-op).
  • Handles non-existent paths: Returns false for paths that don't exist.
  • Path parsing: Supports escaped characters in quoted strings within bracket notation.
  • Array index handling: Numeric strings in paths are treated as array indices when appropriate.

Usage Examples:

var unset = require('lodash.unset');

// Dot notation
var obj1 = { a: { b: { c: 42 } } };
unset(obj1, 'a.b.c');
// => true, obj1 is now { a: { b: {} } }

// Bracket notation with array indices
var obj2 = { users: [{ name: 'Alice', age: 30 }] };
unset(obj2, 'users[0].age');
// => true, obj2 is now { users: [{ name: 'Alice' }] }

// Array path format
var obj3 = { data: { items: [{ id: 1, value: 'test' }] } };
unset(obj3, ['data', 'items', 0, 'value']);
// => true, obj3 is now { data: { items: [{ id: 1 }] } }

// Mixed notation with quoted keys
var obj4 = { 'special-key': { 'another key': 'value' } };
unset(obj4, '["special-key"]["another key"]');
// => true, obj4 is now { 'special-key': {} }

// Handle non-existent paths gracefully
var obj5 = { a: 1 };
unset(obj5, 'b.c.d');
// => false, obj5 remains { a: 1 }

// Handle null/undefined objects
unset(null, 'any.path');
// => true (no-op)
unset(undefined, 'any.path');
// => true (no-op)

Path Resolution Details

The unset function supports sophisticated path parsing:

Supported Path Formats

  1. Dot Notation: 'property.nested.deep'
  2. Bracket Notation: 'property["key"]', 'property[0]'
  3. Mixed Notation: 'property[0].nested["key"]'
  4. Array Paths: ['property', 0, 'nested', 'key']

Special Character Handling

  • Escaped Characters: Supports escaped characters in quoted strings ('key["with \"quotes\""]')
  • Numeric Indices: Automatically treats numeric strings as array indices
  • Special Keys: Handles property names with spaces, hyphens, and special characters

Edge Cases

  • Empty Paths: Empty strings or empty arrays are treated as no-op
  • Invalid Objects: Non-object targets return false
  • Prototype Properties: Only removes own properties, not inherited ones
  • Non-configurable Properties: May return false for non-configurable properties
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.unset@4.5.x
Publish Source
CLI
Badge
tessl/npm-lodash-unset badge