The lodash method _.unset exported as a module for removing object properties at specified paths.
npx @tessl/cli install tessl/npm-lodash--unset@4.5.00
# lodash.unset
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: lodash.unset
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.unset`
10
11
## Core Imports
12
13
```javascript
14
var unset = require('lodash.unset');
15
```
16
17
Note: This package is built for CommonJS and does not provide native ES6 module exports.
18
19
## Basic Usage
20
21
```javascript
22
var unset = require('lodash.unset');
23
24
// Create a test object
25
var object = { 'a': [{ 'b': { 'c': 7 } }] };
26
27
// Remove nested property using dot notation
28
var result = unset(object, 'a[0].b.c');
29
console.log(result); // => true
30
console.log(object); // => { 'a': [{ 'b': {} }] }
31
32
// Remove property using array path
33
var object2 = { 'a': [{ 'b': { 'c': 7 } }] };
34
var result2 = unset(object2, ['a', '0', 'b', 'c']);
35
console.log(result2); // => true
36
console.log(object2); // => { 'a': [{ 'b': {} }] }
37
```
38
39
## Capabilities
40
41
### Property Removal
42
43
Removes the property at the specified path of an object. The method mutates the original object and supports various path formats.
44
45
```javascript { .api }
46
/**
47
* Removes the property at `path` of `object`.
48
*
49
* Note: This method mutates `object`.
50
*
51
* @param {Object} object The object to modify.
52
* @param {Array|string} path The path of the property to unset.
53
* @returns {boolean} Returns `true` if the property is deleted, else `false`.
54
*/
55
function unset(object, path);
56
```
57
58
**Parameters:**
59
- `object` (Object): The object to modify. Can be `null` or `undefined` (treated as no-op).
60
- `path` (Array|string): The path of the property to unset. Supports multiple formats:
61
- Dot notation: `'a.b.c'`
62
- Bracket notation: `'a[0].b.c'`, `'a["key"].b'`
63
- Mixed notation: `'a[0].b["key"].c'`
64
- Array path: `['a', 0, 'b', 'key', 'c']`
65
66
**Returns:**
67
- `boolean`: Returns `true` if the property is deleted, `false` if the property doesn't exist or deletion fails.
68
69
**Behavior:**
70
- **Mutates original object**: The function modifies the input object directly.
71
- **Safe with null/undefined**: Returns `true` for null or undefined objects (no-op).
72
- **Handles non-existent paths**: Returns `false` for paths that don't exist.
73
- **Path parsing**: Supports escaped characters in quoted strings within bracket notation.
74
- **Array index handling**: Numeric strings in paths are treated as array indices when appropriate.
75
76
**Usage Examples:**
77
78
```javascript
79
var unset = require('lodash.unset');
80
81
// Dot notation
82
var obj1 = { a: { b: { c: 42 } } };
83
unset(obj1, 'a.b.c');
84
// => true, obj1 is now { a: { b: {} } }
85
86
// Bracket notation with array indices
87
var obj2 = { users: [{ name: 'Alice', age: 30 }] };
88
unset(obj2, 'users[0].age');
89
// => true, obj2 is now { users: [{ name: 'Alice' }] }
90
91
// Array path format
92
var obj3 = { data: { items: [{ id: 1, value: 'test' }] } };
93
unset(obj3, ['data', 'items', 0, 'value']);
94
// => true, obj3 is now { data: { items: [{ id: 1 }] } }
95
96
// Mixed notation with quoted keys
97
var obj4 = { 'special-key': { 'another key': 'value' } };
98
unset(obj4, '["special-key"]["another key"]');
99
// => true, obj4 is now { 'special-key': {} }
100
101
// Handle non-existent paths gracefully
102
var obj5 = { a: 1 };
103
unset(obj5, 'b.c.d');
104
// => false, obj5 remains { a: 1 }
105
106
// Handle null/undefined objects
107
unset(null, 'any.path');
108
// => true (no-op)
109
unset(undefined, 'any.path');
110
// => true (no-op)
111
```
112
113
## Path Resolution Details
114
115
The `unset` function supports sophisticated path parsing:
116
117
### Supported Path Formats
118
119
1. **Dot Notation**: `'property.nested.deep'`
120
2. **Bracket Notation**: `'property["key"]'`, `'property[0]'`
121
3. **Mixed Notation**: `'property[0].nested["key"]'`
122
4. **Array Paths**: `['property', 0, 'nested', 'key']`
123
124
### Special Character Handling
125
126
- **Escaped Characters**: Supports escaped characters in quoted strings (`'key["with \"quotes\""]'`)
127
- **Numeric Indices**: Automatically treats numeric strings as array indices
128
- **Special Keys**: Handles property names with spaces, hyphens, and special characters
129
130
### Edge Cases
131
132
- **Empty Paths**: Empty strings or empty arrays are treated as no-op
133
- **Invalid Objects**: Non-object targets return `false`
134
- **Prototype Properties**: Only removes own properties, not inherited ones
135
- **Non-configurable Properties**: May return `false` for non-configurable properties