# Array.flat 实现

```javascript
var arr1 = [1, 2, [3, 4]];
arr1.flat(); 
// [1, 2, 3, 4]
```

## 解法

1. 注意第一次写上来就用了map, 是不可以的，原因是输出只有一层\[ ]，中间的迭代不能产生数组

```javascript
Array.prototype.flat = function() {
    var arr = [];
    this.forEach((item,idx) => {
        if(Array.isArray(item)) {
            arr = arr.concat(item.flat()); //递归去处理数组元素
        } else {
            arr.push(item)   //非数组直接push进去
        }
    })
    return arr;   //递归出口
}
```

### 666的解法

仅针对面试题

```javascript
Array.prototype.flat = function (){
    this.toString().split(',').map(item=> +item )
}
```

过程：

1. toString方法，连接数组并返回一个字符串 `'2,2,3,2,3,4'`
2. split方法分割字符串，变成数组`['2','2','3','2','3','4']`
3. map方法，将string映射成为number类型`2,2,3,2,3,4`


---

# Agent Instructions: 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:

```
GET https://mm.ricky.moe/algorithm/algorithm-and-data-structure/di-gui/array.flat-shi-xian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
