# Add Object.splice Method

**URL:** <https://es.discourse.group/t/add-object-splice-method/2166>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [October 6, 2024, 6:04am UTC](https://es.discourse.group/t/add-object-splice-method/2166 "2024-10-06T06:04:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lei](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lei/32/2244_2.png) [@Lei](https://es.discourse.group/u/Lei)\
**Post date:** [October 6, 2024, 6:04am UTC](https://es.discourse.group/t/add-object-splice-method/2166/1 "2024-10-06T06:04:49Z")

</div>

The dynamic management of object properties is a frequent necessity in programming. Currently, using the `delete` operator to remove properties one by one is not flexible enough. To improve the convenience of object manipulation, I propose adding the `Object.splice` method.

## Description

The `Object.splice` method allows developers to delete multiple object properties at once and optionally add new properties. The method returns an object containing all the deleted property key-value pairs.

### Example

```javascript
const myObject = { a: 1, b: 2, c: 3, d: 4 }

// Delete 'b' and 'd', and add new properties 'e' and 'f'
const deleted = Object.splice(myObject, ['b', 'd'], { e: 5, f: 6 })

console.log('Deleted:', deleted) // { b: 2, d: 4 }
console.log('After:', myObject) // { a: 1, c: 3, e: 5, f: 6 }

```

## Syntax

```javascript
Object.splice(obj, keysToDelete, ...itemsToAdd)

```

### Parameters

- `obj`：The object to be manipulated.
- `keysToDelete`：An array of strings containing the keys of properties to delete.
- `...itemsToAdd`：A variable number of objects containing the key-value pairs to add.

### Return Value

- An object containing all the deleted property key-value pairs.

### Throws Exception

- If attempting to delete a non-configurable property, throws a `TypeError` exception.

## Implementation

```javascript
Object.splice = (obj, keysToDelete, ...itemsToAdd) => {
    const deletedItems = {}
    for (const key of keysToDelete) {
        deletedItems[key] = obj[key]
        if (!delete obj[key]) {
            throw new TypeError(`Cannot delete property '${key}' of #<Object>`)
        }
    }
    Object.assign(obj, ...itemsToAdd)
    return deletedItems
}

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 6, 2024, 6:18am UTC](https://es.discourse.group/t/add-object-splice-method/2166/2 "2024-10-06T06:18:46Z")

</div>

I wouldn't personally want to see any new mutating methods be added to the language.

You may be interested in [https://github.com/tc39/proposal-object-pick-or-omit](https://github.com/tc39/proposal-object-pick-or-omit), however.

---

<div class="post-metadata">

**Author:** ![blob](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/blob/32/2230_2.png) [@blob](https://es.discourse.group/u/blob)\
**Post date:** [October 10, 2024, 5:52pm UTC](https://es.discourse.group/t/add-object-splice-method/2166/3 "2024-10-10T17:52:47Z")

</div>

Not sure it's a good idea to implement new features that encourage bad practices  
if you find yourself deleting properties frequently there's a good chance a different data structure like Map is better suited. In other cases, setting the property to null makes more sense. In any and all cases, using `delete` is the clear-cut **_worst_** choice for performance, and a splice method won't fix that
