2014-04-23 70 views
0

我在for循環中動態創建數組。這是完美的作品,但我不知道如何從另一個功能訪問陣列訪問動態創建的數組

$(document).ready(function() { 

var title = "This is a title"; 
var paragraph = "Praesent id metus massa, ut blandit odio."; 

w = window; 

for (i=0; i<10; i++){ 

    w["arr_"+i] = []; 

    w["arr_"+i].push(title + i); 
    w["arr_"+i].push(paragraph); 

    console.log(w["arr_"+i]); 

} 

}); 

function showArray(){ 

//I want to use the array data here 
// this returns "arr3 is not defined " 
console.log(arr3); 

} 

showArray(); 
+1

um您添加到窗口對象的數組屬性是arr_,所以肯定console.log(arr_3)會更合適? – OJay

回答

3

您的代碼中有下劃線。

console.log(arr_3); 
+0

我粘貼代碼時出錯。的console.log(arr_3);仍然返回undefined。謝謝xdazz – andehlu

+0

其實... jquery document.ready正在打破它。固定。 – andehlu