# arr.removeUndefined()

{% tabs %}
{% tab title="💾 程式" %}
```javascript
/**
 * 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;
```

💈範例：

```javascript
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 ]
```
{% endtab %}

{% tab title="👥 相關" %}
* [arr.filter](../method/arr.filter "mention"), [flatmap](../method/flatmap "mention")
* [arr.removevalue](arr.removevalue "mention")
* arr.[filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) ignores "[holes](../sparse)" in array.
{% endtab %}

{% tab title="📗 參考" %}
* [ ] Technie Delight ⟩ [Remove undefined values from an array in JavaScript](https://www.techiedelight.com/remove-falsy-values-from-an-array-in-javascript/)
* [ ] Better Programming ⟩ [What Are Holes in Arrays?](https://betterprogramming.pub/what-are-holes-in-arrays-3ac5fcbcd1c)
{% endtab %}

{% tab title="📘 手冊" %}
* [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
{% endtab %}
{% endtabs %}
