2016-04-16 55 views
1

我目前有一個被稱爲多次的函數,因爲它通過我的程序遞歸。每次該函數被調用時,我都試圖創建一個新的數組。當我的程序失效時,它會運行另一個將項目添加到新陣列中的函數。一旦所有這些項目被添加到該數組中,然後我試圖將該數組推入另一個數組中,該數組存儲包含不同元素的所有不同數組。你能動態地在Javascript中創建一個數組嗎?

例如:

var staticarray = []; 

function createarray(){ 
    if(test == true){ 
     //creates new array 
    } 
    additems(); 
    //pushes array to staticarray 
} 

function additems(){ 
    if(anothertest == true){ 
     //adds items into new array 
    } 
} 

這可能嗎?

+2

如果我們在問這類問題之前開始學習JS,那麼該怎麼辦? http://www.w3schools.com/js/js_arrays.asp –

+0

您提供的代碼太含糊。我看不到任何遞歸發生。乍一看,JavaScript看起來似乎很簡單,但我認爲它會幫助你閱讀一篇很好的介紹,例如https://github.com/getify/You-Dont-Know-JS。 – Chronos

回答

1
// Create array 
let myArray = ["1"]; 
// Add Item 
myArray.push("2"); 

// Add Items of another array 
let otherArray= ["3", "4"]; 
myArray.push([...otherArray]); 
相關問題