> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/builtin/arr/ext.md).

# Array extension

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [object](/web/js/val/obj.md)⟩ [built-in](/web/js/val/builtin.md) ⟩ [Array](/web/js/val/builtin/arr.md) ⟩ extension

{% hint style="success" %}
add custom methods to Array.prototype.
{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}

* replit ⟩ [Array extension](https://replit.com/@pegasusroe/Array-extension#ext/Array_ext.js)

```javascript
// 2023.01.16 - 22:55 - (+) .maxIn()
// 2022.12.29 - 20:14 - (+) .removeDuplicates(), .union(), .isEmpty
// --------------------------------------------------------------------

const { floor, random } = Math;

// ⭐ Array extension
// --------------------------------------------------------------------
// 🔸 .isEmpty
// 🔸 .randomElement         - random element
// 🔹 .removeDuplicates()    - remove duplicate element (new copy)
// 🔹 .union()               - union with unique elements
// 🔹 .maxIn()               - max quantity in array
// --------------------------------------------------------------------
Object.defineProperties(Array.prototype, {

    // 🔸 .isEmpty
    isEmpty: {
        get() {
            return this.length === 0;
        },
    },
    
    // 🔸 .randomElement
    randomElement: {
        get() {
            return this[floor(random() * this.length)];
        },
    },

    // 🔹 .removeDuplicates()
    removeDuplicates: {
        value: function() {
            return [... new Set(this)];    // new array (non-mutating)
        },
    },

    // 🔹 .union()
    union: {
        value: function(...arrays) {
            return this.concat(...arrays).removeDuplicates(); 
        },
    },

    // 🔹 .maxIn()
    // - max quantity in array
    maxIn: {
        value: function(quantity) {
            return this
                .map(x => quantity(x))
                .reduce((max, value) => max = value > max ? value : max);
        },
    },
    
});

// export
module.exports = {};
```

{% endtab %}

{% tab title="💈範例" %}

```javascript
[].isEmpty,               // true
[1,2,3].randomElement,    // (random)
[ 1, 1, 2, 3, 2, 3, 4 ].removeDuplicates(),  // [ 1, 2, 3, 4 ]
[1].concat([1,2,3],[2,3,4]),                 // [ 1, 1, 2, 3, 2, 3, 4 ]
[1].union([1,2,3],[2,3,4]),                  // [ 1, 2, 3, 4 ]

const touchEvents = ['touchstart', 'touchmove', 'touchend'];
const mouseEvents = ['mousedown', 'mouseup', 'click'];
const eventTypes = [...touchEvents, ...mouseEvents];
eventTypes.maxIn(x => x.length)              // 10
```

💈其他範例：

* [touch event](/web/browser/event/type/touch.md) - use array.maxIn(quantity).&#x20;
  {% endtab %}

{% tab title="🔸 屬性" %}

* [arr.isEmpty](/web/js/val/builtin/arr/ext/arr.isempty.md)
* [arr.last](/web/js/val/builtin/arr/ext/arr.last.md)
* [arr.randomElement](/web/js/val/builtin/arr/ext/random.md) - random element.
* [arr.removeDuplicates()](/web/js/val/builtin/arr/ext/arr.removeduplicates.md) - remove duplicate elements (new copy).
  {% endtab %}
  {% endtabs %}
