New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: add Array.prototype.remove #1322

Closed
jolleekin opened this Issue Oct 9, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@jolleekin

jolleekin commented Oct 9, 2018

Surprisingly, Array has always lacked a method to remove an item. To remove an item, one has to use indexOf (or lastIndexOf) and splice, which is very inconvenient. It would be nice if the following method (written in TypeScript here) could be added to Array.

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;
    }
}
@ljharb

This comment has been minimized.

Show comment
Hide comment
@ljharb

ljharb Oct 9, 2018

Member

Please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#new-feature-proposals for how best to suggest new features.

(As an aside, .filter seems like a much better non-mutating approach here than two O(n) operations)

Member

ljharb commented Oct 9, 2018

Please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#new-feature-proposals for how best to suggest new features.

(As an aside, .filter seems like a much better non-mutating approach here than two O(n) operations)

@ljharb ljharb closed this Oct 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment