Array.prototype.remove(item)

Jordan Harband ljharb at gmail.com
Wed Oct 10 04:39:15 UTC 2018


What's the benefit of adding more array mutator methods? The general idioms
of the community have moved towards approaches that create a new object,
rather than changing the old one - ie, .filter would be appropriate here.

On Tue, Oct 9, 2018 at 7:10 PM Man Hoang <jolleekin at outlook.com> wrote:

> There are cases that you only want to remove the first occurrence of an
> item (because you know there is at most one occurrence of that item) and
> there are cases that you want to remove more than one item. Therefore, I
> propose adding the following methods (written in TypeScript here for static
> typing support), optimizing for each use case.
>
>
>
> class Array<E> {
>
>     /**
>
>      * Removes the first occurrence of [item] from this array.
>
>      *
>
>      * Returns `true` if [item] was in this array and `false` otherwise.
>
>      */
>
>     remove(item: E): boolean {
>
>         const i = this.indexOf(item);
>
>         if (i >= 0) {
>
>             this.splice(i, 1);
>
>             return true;
>
>         }
>
>         return false;
>
>     }
>
>
>
>     /**
>
>      * Removes all items that satisfies the predicate [test].
>
>      * @returns The number of removed items.
>
>      */
>
>     removeWhere(test: (item: E) => boolean): number {
>
>         let count = 0;
>
>         for (let i = this.length - 1; i >= 0; i--) {
>
>             if (test(this[i])) {
>
>                 this.splice(i, 1);
>
>                 count++;
>
>             }
>
>         }
>
>         return count;
>
>     }
>
> }
>
>
>
> References
>
>    - [C#] List.Remove
>    <https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.remove?view=netframework-4.7.2>
>    - [C#] List.RemoveAll
>    <https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removeall?view=netframework-4.7.2>
>    - [Dart] List.remove
>    <https://api.dartlang.org/stable/2.0.0/dart-core/List/remove.html>
>    - [Dart] List.removeWhere
>    <https://api.dartlang.org/stable/2.0.0/dart-core/List/removeWhere.html>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181009/f3951e8f/attachment.html>


More information about the es-discuss mailing list