如何循环播放&显示Javascript多维数组

时间:2013-08-07 19:10:31

标签: javascript

我有一个测试搜索软件,它将数据存储在一个多维数组中。我可以返回整个数据库,但不能只返回一个值。我试图弄清楚如何将一个部分作为多维数组返回。否则它只是在显示屏上重复传递的值。在解决问题的同时,我可以看到完整的数组存储为参数,但我无法弄清楚如何循环遍历该数组以正确显示。您可能需要查看源以更好地理解。如果您输入让我们说439023483并单击按ISBN搜索按钮,您将看到我的问题。 show all按钮工作正常。任何指向我正确方向的东西都将非常感谢,并提前感谢你。

以下是测试来源的链接: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);
}

2 个答案:

答案 0 :(得分: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]);

答案 1 :(得分: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