# Array.prototype.without

**URL:** https://es.discourse.group/t/array-prototype-without/2092
**Category:** 💡 Ideas
**Tags:** proposal
**Created:** [July 21, 2024, 7:51pm UTC](https://es.discourse.group/t/array-prototype-without/2092 "2024-07-21T19:51:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![elan.hamburger](https://avatars.discourse-cdn.com/v4/letter/e/82dd89/32.png) [@elan.hamburger](https://es.discourse.group/u/elan.hamburger)
#### Post date: [July 21, 2024, 7:51pm UTC](https://es.discourse.group/t/array-prototype-without/2092/1 "2024-07-21T19:51:22Z")

</div>

ES2023 introduced `Array.prototype.with` which returns a copy of the array with the element at the specified index replaced with a specified value. This is very useful when working in React or other environments where having immutable variables is important. However, there are other array mutations that one may want to perform without modifying the original array.

I would like to propose `with`'s counterpart, `without` which would return a copy of the array with the element at the specified index removed. This would essentially be a shorthand for

```javascript
myArray.filter((_, index) => index !== indexToRemove);

```

With an implementation of `without` available, a user could equivalently write

```javascript
myArray.without(indexToRemove)

```

which is much more concise, more clearly expresses the programmer's intent, and doesn't require the unused binding within the callback to `filter`.

If desired, the implementation could also accept multiple indices to remove.

```javascript
Array.prototype.without = function(...indicesToRemove) {
    return this.filter((_, index) => !indicesToRemove.includes(index));
}

```

---

<div class="post-metadata">

### Author: ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)
#### Post date: [July 21, 2024, 9:13pm UTC](https://es.discourse.group/t/array-prototype-without/2092/2 "2024-07-21T21:13:07Z")

</div>

You can use [the `toSpliced` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced) for that:

```js
myArray.toSpliced(indexToRemove, 1)

```

---

<div class="post-metadata">

### Author: ![oleedd](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/oleedd/32/2148_2.png) [@oleedd](https://es.discourse.group/u/oleedd)
#### Post date: [July 21, 2024, 10:06pm UTC](https://es.discourse.group/t/array-prototype-without/2092/3 "2024-07-21T22:06:39Z")

</div>

Default value 1 should be there for convenience. Again no default values.

---

<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: [July 21, 2024, 10:21pm UTC](https://es.discourse.group/t/array-prototype-without/2092/4 "2024-07-21T22:21:37Z")

</div>

why would `1` make any sense as the default value? it's not the front, and not necessarily the back, it's just an arbitrary number. doesn't seem very convenient to me.

---

<div class="post-metadata">

### Author: ![oleedd](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/oleedd/32/2148_2.png) [@oleedd](https://es.discourse.group/u/oleedd)
#### Post date: [July 22, 2024, 7:53am UTC](https://es.discourse.group/t/array-prototype-without/2092/5 "2024-07-22T07:53:51Z")

</div>

For cases like this. 1 is in most cases. It is the minimum value if you remove. Many functions have minimum values by default, for example index.

---

<div class="post-metadata">

### Author: ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)
#### Post date: [July 22, 2024, 8:09am UTC](https://es.discourse.group/t/array-prototype-without/2092/6 "2024-07-22T08:09:29Z")

</div>

The argument is already optional, but it does not default to `1`. So this can't be changed now.

> If `deleteCount` is omitted, or if its value is greater than or equal to the number of elements after the position specified by `start` , then all the elements from `start` to the end of the array will be deleted. However, if you wish to pass any `itemN` parameter, you should pass `Infinity` as `deleteCount` to delete all elements after `start` , because an explicit `undefined` gets [converted](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#integer_conversion) to `0` .

> **[Array.prototype.toSpliced() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced)**
>
> The toSpliced() method of Array instances is the copying version of the splice() method. It returns a new array with some elements removed and/or replaced at a given index.
