2013-08-07 102 views
0

我有一個測試搜索軟件,它將數據存儲在多維數組中。我可以返回整個數據庫,但不能只返回一個值。我試圖找出如何將一個部分作爲多維數組返回。否則,它只是重複顯示屏上傳遞的值。雖然解開它,我可以看到完整的數組存儲爲一個參數,但無法找出如何循環訪問該數組以正確顯示。您可能需要查看來源才能更好地理解。如果您輸入讓我們說439023483並點擊按ISBN搜索按鈕,您將看到我的問題。顯示全部按鈕正常工作。任何指向我的方向正確的方向將不勝感激,並提前致謝。如何循環顯示Javascript Multidimensional Array

下面是測試源鏈接:http://mdhmotors.com/jstesting/test.html

這裏是代碼的一部分,我是卡在:

function searchByISBN(isbn) 
{ 
    var isbn = document.getElementById('isbn').value; 
    showBooks(getBookByIsbn(isbn)); 

} 
function getBookByIsbn(isbn) 
{ 
    var foundBook = null; 
    for (b in BookStore) 
    { 
     var book = BookStore[b]; 
     if (book[ISBN] == isbn) 
     { 
      foundBook = new Array(book[ISBN], book[TITLE], book[AUTHOR], book[PUBLISHER], book[WS_PRICE], book[RET_PRICE], book[QUANTITY]); 
      break; 
     } 
    } 
    return foundBook; 
} 

/*display search results*/ 
function showBooks(searchResults) 
{ 
    //clearDisplayTable(); 
    if (searchResults == null) 
    { 
     return; 
    } 
    var row, cell, displayTable, tableHeader, tableTitle; 
    displayTable = document.createElement('table'); 
    displayTable.className = "mytable"; 
    tableBody = document.createElement('tbody'); 
    row = document.createElement('tr'); 
    tableHeader = document.createElement('th'); 
    tableHeader.appendChild(document.createTextNode("Book Store Inventory")); 
    tableHeader.setAttribute('colSpan', 8); 
    tableHeader.setAttribute('style', 'font-size: 22px'); 
    row.appendChild(tableHeader); 
    tableBody.appendChild(row); 

row = document.createElement('tr'); 
columnName = document.createElement('th'); 
columnName.appendChild(document.createTextNode("#")); 
row.appendChild(columnName); 

columnName = document.createElement('th'); 
columnName.appendChild(document.createTextNode("Isbn")); 
row.appendChild(columnName); 

columnName = document.createElement('th'); 
columnName.appendChild(document.createTextNode("Title")); 
row.appendChild(columnName); 

columnName = document.createElement('th'); 
columnName.appendChild(document.createTextNode("Author")); 
ow.appendChild(columnName); 

columnName = document.createElement('th'); 
columnName.appendChild(document.createTextNode("Publisher")); 
row.appendChild(columnName); 

columnName = document.createElement('th'); 
columnName.appendChild(document.createTextNode("W/S")); 
row.appendChild(columnName); 

columnName = document.createElement('th'); 
columnName.appendChild(document.createTextNode("Retail")); 
row.appendChild(columnName); 

columnName = document.createElement('th'); 
columnName.appendChild(document.createTextNode("Qty")); 
row.appendChild(columnName); 
tableBody.appendChild(row); 

    var count = 0; 
    for (b in searchResults) 
    { 
    row = document.createElement('tr'); 
    book = searchResults[b]; 
    var data = new Array(++count, book[ISBN], book[TITLE], book[AUTHOR], book[PUBLISHER], book[WS_PRICE], book[RET_PRICE], book[QUANTITY]); 
     for (var index = 0; index < 8; index++) 
     { 
      cell = document.createElement('td'); 
      cellText = document.createTextNode(data[index]); 

      if (index == 0) 
      cell.setAttribute('style', 'text-align: right'); 
      cell.appendChild(cellText); 
      row.appendChild(cell); 
     } 
    tableBody.appendChild(row); 
    } 
displayTable.appendChild(tableBody); 
bookResults.appendChild(displayTable); 
} 
+1

你不需要那麼長的代碼只是問如何迭代mul。數組... –

+0

JavaScript中沒有這種「多維數組」。只有數組的值也可以是數組。我認爲這不是太挑剔或迂腐,而是因爲它可以讓你對你需要做的事情有所瞭解。 – Phrogz

回答

3

要在多維數組返回值:array[n][m]是第n行的第m個元素。要爲每一個元素做到這一點,使用內嵌的for循環:

for (var i = 0; i < array.length; i++){ 
    for (var j = 0; j < array[i].length; j++){ 
     console.log(array[i][j]); 
+0

我似乎無法弄清楚在哪裏循環foundBook數組,以便正確顯示。它返回ISBN,我可以看到watchstack中所有書屬性的完整數組。但它只是循環通過ISBN號碼。 – handmdmr

0

使用jQuery - 但很容易適應純JS:

var my_multi_dimensional_array = ["a", "b", "c", ["d", "e", "f"], "g", ["h", ["i"], 
    ["j", "k"], "l", ["m", ["n", "o", [ 
     [ 
      ["p"], "q"] 
    ]]] 
]]; 

(function walkies(arr, path, callback, root) { 
    $.each(arr, function (i, v) { 
     //push index onto path stack 
     path.push(i); 
     var recurseable = ($.isArray(v) || $.isPlainObject(v)) ; 
     var recurse = callback.call(v, !recurseable, v, path, i, root || arr) && recurseable; 

     //call callback and continue recursing this node until callback returns false 
     if (recurse) { 
      walkies(v, path, callback, root || arr); 
     } 

     //pop last index off path stack 
     path.pop(); 
    }); 
}(my_multi_dimensional_array, [], function (isLeaf, node, path, index, root) { 
    if(isLeaf){ 
     console.log("[" + path.join("],[") + "]=" + node); 
    } 
    return true; 
})); 

jsFiddle