假設,我們所需要的二維陣列長得像這個樣子:
var tempArray = {{"HTML5", "90", "A+"}, {"jQuery", "60", "C-"}, {"Ruby", "72", "B-"}, {"Perl", "83", "B"}};其實,遇到這種實務上的情形,我們該如何宣告?? 這種4 * 3的二維陣列,我們可以用3個一維陣列來分別宣告:
// 三個原始資料的一維陣列 var data1 = new Array("HTML5", "jQuery", "Ruby", "Perl"); var data2 = new Array(90, 60, 72, 83); var data3 = new Array("A+", "C-", "B-", "B"); var tempArray = function(ROW, COL) { var virArray = new Array(ROW); $.each(virArray, function(index, context) { virArray[index] = new Array(COL); virArray[index][0] = data1[index]; virArray[index][1] = data2[index]; virArray[index][2] = data3[index]; return; }); return virArray; };執行結果如下:
相關文章:
How to declare a Two-Dimensional Array with the jQuery (I)
How to declare a Two-Dimensional Array with the jQuery (II)
沒有留言:
張貼留言