Javascript,表和数组。棘手的问题

时间:2012-06-03 16:14:52

标签: javascript arrays

我正在制作一个书店网站(针对一个小型的大学javascript项目)到目前为止,我所有的书籍信息都在.js文件中的数组中,看起来像这样......

var headings  = new Array(4);
headings [0] = "title"
headings [1] = "author"
headings [2] = "cat"
headings [3] = "bookid"
headings [4] = "cost"

var bookid   = new Array(6);
var title   = new Array(6);
var cat  = new Array(6);
var author  = new Array(6);
var cost  = new Array(6);
var desc  = new Array(6);

bookid [0] = "0001"
title [0]    = "For whom the tell bowls";
cat[0]   = "fiction";
author[0]   = "John Doe";
cost[0]   = "€12.99";
desc[0]   = "This book is frickin' amazing blah blah blah"

等......有7本书......

现在我想生成一个表格,当我点击一个列中的作者,标题,成本等缩略图时,另一列中的实际相应详细信息,这是代码

for(var j = 0; j < 5; j++) {
                row = document.createElement("tr");

                // Each with 2 cells
                for(var i = 0; i < 2; i++) {

                    var cell = document.createElement("td");

                    var temp = headings[j];
                    var temp2 = (temp + '[' + filename + ']');
                    if (i==1)
                    {var text = document.createTextNode(temp2);
                    }
                    else 
                    {var text = document.createTextNode(temp);}

btw“filename”被传递给这个函数,它是书的#,例如,0,1,2等等...... 现在,页面上显示的内容是正确的..对于书0我得到..

title   title[0]
author  author[0]
cat         cat[0]
bookid  bookid[0]
cost    cost[0]

对于第1本书我得到了

title   title[1]
author  author[1]
cat         cat[1]
bookid  bookid[1]
cost    cost[1]

等等,但细节不会被解释为数组中的实际变量。我猜这个解决方案与“innerHTML”有关,或者可能以某种方式投射我的变量..

如果有人知道这方面的解决方案,我将非常感激!我计划今天完成这个网站,并转向其他更重要的事情(因为这个项目不值得多分)但是留下这个漏洞在那里会让我烦恼!

2 个答案:

答案 0 :(得分:3)

您现在解决此问题的方法是(尝试)按名称访问变量,例如:您想使用"title"来查找名为title的变量。 不要这样做。您最终会污染全局范围并依赖window来访问这些变量,否则您将落入 {{ 1}}恶魔

这是一个更好的主意:制作用于存储书籍的对象。一本书看起来像:

eval

更好的方法是定义一种书籍:

var book = {
    title: "Foo",
    author: "Bar",
    cat: "Baz",
    bookid: 123456,
    cost: "One hundred billion dollars"
};

您可以将这些图书存储在一个图书数组中:

function Book(title, author, cat, bookid, cost) {
    this.title = title;
    this.author = author;
    this.cat = cat;
    this.bookid = bookid;
    this.cost = cost;
}
var book = new Book("Foo", "Bar", "Baz", 123456, "One hundred billion dollars");
突然间,桌子变得容易了!

var books = [book1, book2, book3, ...];

您甚至可以使用一个book元素创建表格标题:

// Create or get a table
var table = document.createElement("table");

// Loop over the books array
for (var i=0, l=books.length; i < l; ++i) {
    // Get the current book
    var book = books[i];

    // Create a table row
    var row = document.createElement("tr");

    // Loop over the *properties* of the book
    for (var propertyName in book) {
        // Skip inherited properties
        if(!book.hasOwnProperty(propertyName)) continue;

        // Get the *property value*
        var property = book[propertyName];

        // Create a table cell and append to the row
        var textNode = document.createTextNode(property);
        var cell = document.createElement("td");
        cell.appendChild(textNode);
        row.appendChild(cell);
    }
    // Append the row to the table
    table.appendChild(row);
}

更新:更好地控制标题

正如丹尼斯所说,你可能想要更多地控制标题。您不希望将// Some book to loop over its property names var someBook = books[0]; var row = document.createElement("tr"); // Loop over the book properties for(var propertyName in someBook) { // Now placing the *property name* in a text node var textNode = document.createTextNode(propertyName); var header = document.createElement("th"); cell.appendChild(textNode); row.appendChild(header); } table.appendChild(row); 显示为图书类别的标题,您希望更具可读性。此外,您可能不希望显示存储在书籍对象上的所有属性。在这种情况下,您可以存储标题以显示在另一个对象中,并添加有关这些标题的更多信息:

"cat"

在数据循环中,您将迭代var headings = { title: { text: "Book title" }, author: { text: "Author" }, cat: { text: "Category" }, bookid: { text: "ID" }, cost: { text: "Price" } }; 而不是headings的属性。

book

在标题循环中,您可以使用// Loop over the properties to display for (var propertyName in headings) { // Get the property value var property = book[propertyName]; // [snipped] } 作为显示名称,而不只是headings[propertyName].text。您不再需要书籍实例,因此可以删除propertyName变量。

someBook

答案 1 :(得分:1)

你的问题在这里:

var temp2 = (temp + '[' + filename + ']');

你是连接字符串;而不是JavaScript语句title[1],最终得到字符串"title[1]"

你应该做的是使用构造函数创建一个对象并构建一个对象数组,而不是维护几个parallel arrays

function Book(title, author, cat, id, cost) {
    this.title = title;  //for each variable
}

var arrayOfBooks = [];
arrayOfBooks.push( new Book( "The Great Gatsby", "F. Scott Fitzgerald", "Fiction", 1, 5000.00));
//and so on for each book.

构建表时:

var book = arrayOfBooks[filename];
var temp = headings[j];
var temp2 = book[temp]; //Use brackets instead of concatenating strings.

另一个提示:如果你只是用if语句切换,那么内部循环是不必要的。您可以创建两个单元格并在每个单元格中放置必要的文本值。

相关问题