๐Ÿ’พarr.removeValue()

removes specific value from array.

/**
 * remove specific `value` from the array.
 * @example
 * import array_removeValue from './.../array_removeValue.mjs';
 * Object.assign(Array.prototype, array_removeValue);
 * @mixin
 */
const array_removeValue = {

    /**
     * remove specific `value` from the array.
     * @example
     * const arr = [1, 2, 1, 3, 1, 4]
     * arr.removeValue(1)     // [ 2, 3, 4 ]
     * @param value {*} the value to be removed
     */
    removeValue(value) {
        return this.filter(x => x !== value);
    },

};

export default array_removeValue;

๐Ÿ’ˆ็ฏ„ไพ‹๏ผš

import array_removeValue from '../objects/mixin/array_removeValue.mjs';
Object.assign(Array.prototype, array_removeValue);

const arr = [1, 2, 1, 3, 1, 4]
arr.removeValue(1)                // [ 2, 3, 4 ]

Archive

/**
 * common tools for Array
 * @mixin
 */
const ArrayTools = {

    /**
     * remove every `value` from the array IN PLACE. โญ๏ธ 
     * @example
     * // extend Array by mixin
     * Object.assign(Array.prototype, ArrayTools);
     * const a = [1, 2, 1, 3, 1, 4];
     * a.removeAll(1);      // [ 2, 3, 4 ]
     * @param value {*} the value to be removed
     */
    removeAll(value) {
        for (let i = 0; i < this.length; i++) {
            if (this[i] === value) {
                this.splice(i, 1);        // remove one item (in place)
                i -= 1;                   // keep i the same in the next loop
            }
        }
    }

};

๐Ÿ’ˆ็ฏ„ไพ‹๏ผš

const a = [1, 2, 1, 3, 1, 4];
Object.assign(Array.prototype, ArrayTools);
a.removeAll(1);
a;                // [2,3,4]

Last updated