Roshin's blog Roshin's blog
首页
  • 《数据结构与算法》笔记
  • 手写代码
  • 实用方法
  • 文档教程
  • 面试集锦
  • 分类
  • 标签
  • 归档
  • 个人介绍
  • 关于博客
  • 友情链接
GitHub (opens new window)

Roshin

如果你只做自己能力范围之内的事情,就永远没法进步。
首页
  • 《数据结构与算法》笔记
  • 手写代码
  • 实用方法
  • 文档教程
  • 面试集锦
  • 分类
  • 标签
  • 归档
  • 个人介绍
  • 关于博客
  • 友情链接
GitHub (opens new window)
  • 手写代码

    • Array.prototype.map()
    • Array.prototype.reduce()
    • Array.prototype.flat()
      • Array.prototype.flat()
      • 语法
      • 参数
      • 返回值
      • 示例
      • 手写实现
        • 使用递归
        • 使用 Array.prototype.reduce()
        • flat([depth])
    • instanceof 运算符
  • 实用方法

  • JavaScript
  • 手写代码
roshin
2021-04-14

Array.prototype.flat()

Array.prototype.flat() #

flat() 方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。

语法 #

arr.flat([depth]);
1

参数 #

  • depth(可选): 指定要提取嵌套数组的结构深度,默认值为 1。

返回值 #

一个包含将数组与子数组中所有元素的新数组。

示例 #

const arr1 = [1, 2, [3, 4]];
console.log(arr1.flat()); // [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]];
console.log(arr2.flat()); // [1, 2, 3, 4, [5, 6]]

const arr3 = [1, 2, [3, 4, [5, 6]]];
console.log(arr3.flat(2)); // [1, 2, 3, 4, 5, 6]

// 使用 Infinity,可展开任意深度的嵌套数组
const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
console.log(arr4.flat(Infinity)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1
2
3
4
5
6
7
8
9
10
11
12

手写实现 #

使用递归 #

function flat(array = []) {
  let result = []; // 定义一个空数组用作保存数据使用

  for (let i = 0; i < array.length; i++) {
    const current = array[i];
    if (Array.isArray(current)) {
      result = result.concat(flat(current));
      continue;
    }
    result.push(current);
  }
  return result;
}

const arr = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
console.log(flat(arr)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

使用 Array.prototype.reduce() #

function flat(array = []) {
  const result = array.reduce((accumulator, currentValue) => {
    return accumulator.concat(Array.isArray(currentValue) ? flat(currentValue) : currentValue);
  }, []);
  return result;
}

const arr = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
console.log(flat(arr)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1
2
3
4
5
6
7
8
9

flat([depth]) #

Array.prototype.myFlat = function flat(depth = 1) {
  const array = this; // 拿到当前数组

  // 首先判断传入的参数是否为数字, 如果不是则直接抛出
  if (isNaN(depth) || depth <= 0) {
    return array.slice();
  }

  depth = Math.floor(depth); // 向下取整拿到一个整数
  let result = []; // 定义一个空数组用作保存数据使用

  for (let i = 0; i < array.length; i++) {
    const current = array[i];
    if (Array.isArray(current)) {
      result = result.concat(current.myFlat(depth - 1));
      continue;
    }
    result.push(current);
  }
  return result;
};

const arr1 = [1, 2, [3, 4]];
console.log(arr1.myFlat()); // [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]];
console.log(arr2.myFlat()); // [1, 2, 3, 4, [5, 6]]

const arr3 = [1, 2, [3, 4, [5, 6]]];
console.log(arr3.myFlat(2)); // [1, 2, 3, 4, 5, 6]

// 使用 Infinity,可展开任意深度的嵌套数组
const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
console.log(arr4.myFlat(Infinity)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
编辑 (opens new window)
上次更新: 2021-06-23 08:43:55
Array.prototype.reduce()
instanceof 运算符

← Array.prototype.reduce() instanceof 运算符→

最近更新
01
函数柯里化
04-15
02
Array.prototype.reduce()
04-14
03
instanceof 运算符
04-13
更多文章>
Theme by Vdoing Copyright © 2021-present Roshin | MIT License | 粤ICP备18116277号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式