Created
June 20, 2021 17:25
-
-
Save PachVerb/4fa9a89bb96a3aa845e0553a04f0ddd0 to your computer and use it in GitHub Desktop.
Revisions
-
PachVerb created this gist
Jun 20, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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]