๐Ÿ’พarr.removeUndefined()

/**
 * remove `undefined` elements (and "holes") from array
 * @example
 * import array_removeUndefined from './.../array_removeUndefined.mjs';
 * Object.assign(Array.prototype, array_removeUndefined);
 * @mixin
 */
const array_removeUndefined = {

    /**
     * remove `undefined` elements (and "holes") from array
     * @example
     * let arr = [1, undefined, 2, , 3]
     * arr.removeUndefined()    // [ 1, 2, 3 ]
     */
    removeUndefined() {
        return this.filter(x => x !== undefined);
    },

};

export default array_removeUndefined;

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

import array_removeUndefined from './.../array_removeUndefined.mjs';
Object.assign(Array.prototype, array_removeUndefined);

const arr = [1, 2, 1,  , 3, 1, 4, undefined];
arr.removeUndefined()    // [ 1, 2, 1, 3, 1, 4 ]

Last updated