> 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/array-and-matrix-methods.md).

# array & matrix methods

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

```javascript
const array_matrix_methods = {
    
    // ---------------------
    //     array methods
    // ---------------------
    
    // arr.isEmpty
    get isEmpty() { return this.length === 0 },
    
    // arr.last
    get last() { return this[this.length - 1] },
    
    // arr.max(f): f must be a real-valued function
    max(f=x=>x){ return Math.max(...this.map(x => f(x)))},

    // ------------------------
    //     2D array methods
    // ------------------------

    // assuming:
    // - elements in array are themselves arrays.
    // - an element in array is called a "row".

    // arr2d.numberOfRows
    get numberOfRows() { return this.length },
    
    // arr2d.maxRowLength
    get maxRowLength() { return this.max(row => row.length) },

    // arr2d.maxColumnIndex
    get maxColumnIndex() { return this.maxRowLength - 1 },

    // arr2d.transpose
    get transpose() {
        return Array.from({length: this.maxRowLength}, (_, j) => 
            this.map(row => row[j])
        );
    },

    // arr2d.map2D()
    map2D(f) {
        return this.map((row, i) => 
            row.map((value, j) => f(value, i, j))
        )
    },

    // ---------------------
    //     matrix methods
    // ---------------------

    // assuming:
    // - every element (row) is still an array of the same length

    // matrix.numberOfColumns
    get numberOfColumns() { return this[0].length },
};
```

{% endtab %}

{% tab title="💈範例" %}
⬆️ 需要： [.assignDescriptors()](/web/js/val/obj/extend/mixin/.assigndescriptors.md)

* [replit](https://replit.com/@pegasusroe/array-and-matrix-methods#index.js)

```javascript
// ⭐ extending Array.prototype
Object.assignDescriptors(Array.prototype, array_matrix_methods);

// test data
let arr = [1, 2, 5, 50, 10];
let filtered = arr.filter(x => x >= 10);    //

let arr2d = [
    [1,2,3],
    [1,2,3,4],
    [1,2]
];

// ---------------- output -----------------
arr.isEmpty,             // false
arr.last,                // 10
arr.max(),               // 50

filtered,                // [ 50, 10 ]
filtered.isEmpty,        // false

arr2d.numberOfRows,      // 3
arr2d.maxRowLength,      // 4

arr2d.map2D((_, i, j) => i === j ? 1 : 0),
// [ [ 1, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0 ] ]

arr2d.transpose,
// [
//   [ 1, 1, 1 ],
//   [ 2, 2, 2 ],
//   [ 3, 3, undefined ],
//   [ undefined, 4, undefined ]
// ]
```

{% endtab %}

{% tab title="📘 手冊" %}

* [Object.defineProperty()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/val/builtin/arr/array-and-matrix-methods.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
