zoukankan      html  css  js  c++  java
  • 542 Array.prototype.flat 与 flatMap

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>flat 与 flatMap</title>
    </head>
    
    <body>
      <script>
        // flat 平
        // 将多维数组转化为低维数组
        const arr1 = [1, 2, 3, 4, [5, 6]];
        console.log(arr1.flat()) //  [1, 2, 3, 4, 5, 6]
    
        const arr2 = [1, 2, 3, 4, [5, 6, [7, 8, 9]]];
        // 参数为深度 是一个数字
        console.log(arr2.flat(2)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
    
        const arr3 = [1, 2, 3, 4, [5, 6, [7, [8, 9]]]];
        console.log(arr3.flat(Infinity)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
    
        // flatMap:结合了flat、map两步操作。
        const arr4 = [1, 2, 3, 4];
        const result = arr4.flatMap(item => [item * 10]);
        console.log(result); // [10, 20, 30, 40]
      </script>
    </body>
    
    </html>
    
  • 相关阅读:
    作业3-2
    作业3-1
    习题二(8)
    习题二(7)
    习题二(6)
    习题二(5)
    习题二(4)
    实验2-2
    实验2
    第二章 例题2-11
  • 原文地址:https://www.cnblogs.com/jianjie/p/13680147.html
Copyright © 2011-2022 走看看