我有一個測試搜索軟件,它將數據存儲在多維數組中。我可以返回整個數據庫,但不能只返回一個值。我試圖找出如何將一個部分作爲多維數組返回。否則,它只是重複顯示屏上傳遞的值。雖然解開它,我可以看到完整的數組存儲爲一個參數,但無法找出如何循環訪問該數組以正確顯示。您可能需要查看來源才能更好地理解。如果您輸入讓我們說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);
}
你不需要那麼長的代碼只是問如何迭代mul。數組... –
JavaScript中沒有這種「多維數組」。只有數組的值也可以是數組。我認爲這不是太挑剔或迂腐,而是因爲它可以讓你對你需要做的事情有所瞭解。 – Phrogz