💾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