js-helper-functions
Advanced tools
Comparing version
{ | ||
"name": "js-helper-functions", | ||
"version": "2.0.11", | ||
"version": "2.0.12", | ||
"main": "dist/js-helper-functions.es.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/js-helper-functions.umd.js", |
@@ -8,3 +8,3 @@ ### Javascript Utility Functions | ||
> **countItemsInMultidimensionalObject** | ||
### > **countItemsInMultidimensionalObject** | ||
@@ -130,1 +130,79 @@ --- | ||
- Leading and trailing dashes are removed for cleanliness. | ||
### > **removeItemFromMultidimensionalObject** | ||
--- | ||
A recursive utility function that removes a specific item from a deeply nested object structure where values can be arrays or further nested objects. | ||
##### ✨ Features | ||
- Traverses and modifies deeply nested objects. | ||
- Removes items from nested arrays based on a given path. | ||
- Cleans up empty branches by removing keys that become empty after item removal. | ||
##### **Data Type** | ||
```ts | ||
type NestedObject = { | ||
[key: string]: Node | string[]; | ||
}; | ||
``` | ||
##### **🏗️ Parameters** | ||
| Name | Type | Description | | ||
| -------------- | -------------- | ------------------------------------------------------------- | | ||
| `obj` | `NestedObject` | The nested object from which to remove the item. | | ||
| `path` | `string[]` | An array representing the path to the array in the structure. | | ||
| `itemToRemove` | `string` | The item to be removed from the array at the given path. | | ||
**📤 Returns** | ||
| Type | Description | | ||
| -------------- | --------------------------------------------- | | ||
| `NestedObject` | A new object with the specified item removed. | | ||
- If the array becomes empty after removal, its key is deleted from the object. | ||
- If any parent objects become empty due to cascading deletions, those are also removed. | ||
#### 📘 Example | ||
```ts | ||
const nested = { | ||
category1: { | ||
subcategory1: ["item1", "item2"], | ||
subcategory2: ["item3"], | ||
}, | ||
category2: { | ||
subcategory3: ["item4"], | ||
}, | ||
}; | ||
const result = removeItemFromMultidimensionalObject( | ||
nested, | ||
["category1", "subcategory1"], | ||
"item2" | ||
); | ||
console.log(result); | ||
``` | ||
#### output: | ||
```ts | ||
{ | ||
category1: { | ||
subcategory1: ['item1'], | ||
subcategory2: ['item3'] | ||
}, | ||
category2: { | ||
subcategory3: ['item4'] | ||
} | ||
} | ||
``` | ||
#### 🔄 Use Cases | ||
- Tree-view item deselection | ||
- Tag or category removal in nested data structures | ||
- Form data cleanup |
14277
16.5%207
60.47%