Array.flat()
是 JavaScript 中的一個內建函數,用於將多維數組(陣列)轉換成一維數組。這個方法在處理多層嵌套數組時特別有用,它將嵌套的數組內的元素提取出來,並將它們放入一個新的一維數組中。這個函數可以接受一個整數參數,表示要扁平化的嵌套層級。
以下是 Array.prototype.flat()
的基本用法和一些示例:
在上面的示例中,nestedArray
包含了多層嵌套的數組,但 flat()
方法將它們轉換為一個一維數組 flatArray
。
flat()
方法可以接受一個整數參數,用來指定要扁平化的嵌套層級。例如:
在上面的示例中,我們通過將 2
傳遞給 flat()
方法,只扁平化了前兩層嵌套。
需要注意的是,如果你不指定參數,flat()
默認將扁平化所有的嵌套層級。
這是一個方便的方法,可以幫助你處理數組中的嵌套數據,使其更容易進行操作和處理。請注意,flat()
方法在ES6中引入,因此在較舊的JavaScript環境中可能不可用。
Flatten Deeply Nested Array
多維數組是包含整數或其他多維數組的遞歸數據結構。
A multi-dimensional array is a recursive data structure
that contains integers or other multi-dimensional arrays.
展平數組是該數組的一個版本,刪除了部分或全部子數組並替換為該子數組中的實際元素。
A flattened array is a version of that array with some or all of the sub-arrays
removed and replaced with the actual elements in that sub-array.
僅當前嵌套深度小於“n”時才應執行此展平操作。
This flattening operation should only be done
if the current depth of nesting is less than n
.
第一個數組中元素的深度被視為“0”。
The depth of the elements in the first array are considered to be 0
.
請在不使用內置 Array.flat
方法的情況下解決它。
Please solve it without the built-in Array.flat
method.
先了解多維數組、遞迴、嵌套深度等定義
Array.flat
方法的情況下解決它。宣告函式flat,接受一個多維數組arr
和變數n
用以設定展平深度
使用for迴圈遍歷多維陣列arr
,判斷要進行遞迴還是元素放進answer
陣列
如果 n>0 且arr
當前遍歷元素是陣列時進行遞迴
如果 n≤0 就把當前遍歷元素放進answer
陣列
遞迴邏輯處理
遞迴解析1
遞迴解析2
Array Wrapper
創建一個類“ArrayWrapper”,在其構造函數中接受整數數組。 這個類應該有兩個特點:
Create a class ArrayWrapper
that accepts an array of integers in its constructor. This class should have two features:
當使用“+”運算符將此類的兩個實例相加時,結果值是兩個數組中所有元素的總和。
When two instances of this class are added together with the +
operator, the resulting value is the sum of all the elements in both arrays.
當在實例上調用“String()”函數時,它將返回一個用括號括起來的逗號分隔字符串。 例如,“[1,2,3]”。
When the String()
function is called on the instance, it will return a comma separated string surrounded by brackets. For example, [1,2,3]
.