Skip to content

Instantly share code, notes, and snippets.

@PachVerb
Created June 20, 2021 17:25
Show Gist options
  • Select an option

  • Save PachVerb/4fa9a89bb96a3aa845e0553a04f0ddd0 to your computer and use it in GitHub Desktop.

Select an option

Save PachVerb/4fa9a89bb96a3aa845e0553a04f0ddd0 to your computer and use it in GitHub Desktop.

Revisions

  1. PachVerb created this gist Jun 20, 2021.
    19 changes: 19 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    let isArray = (arr) => arr instanceof Array;

    function flaten(arr) {
    if (arr.length === 0) return arr;
    // 1. 分解:先找数组

    if (isArray(arr)) {
    arr.forEach((val) => {
    flaten(val);
    });
    } else {
    curr.add(arr);
    }
    return curr;
    }

    // test
    // arr = [1, 2, [3, [4]], 5];
    let res = flaten(arr) // ---> [1, 2, 3, 4, 5]