gpt4 book ai didi

JavaScript、表和数组。棘手的问题

转载 作者:行者123 更新时间:2023-11-28 16:17:48 24 4
gpt4 key购买 nike

我正在制作一个书店网站(用于一个小型大学 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);}

顺便说一句,“文件名”被传递给这个函数,它是这本书的#,例如,0、1、2等等......现在,页面上显示的内容是正确的.. 对于第 0 本书,我得到了..

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

对于第一本书,我得到了

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

等等,但详细信息不会被解释为数组中的实际变量。我猜测该解决方案与“innerHTML”有关,或者可能以某种方式转换我的变量..

如果有人知道这个问题的解决方案,我们将不胜感激!...我计划今天完成这个网站,然后转向其他更重要的事情(因为这个项目不值得很多分)但是留下这个缺陷在那里会打扰我!

最佳答案

您现在解决这个问题的方法是(尝试)通过名称访问变量,例如您想使用 "title" 查找名为 title 的变量。 不要这样做。您最终会污染全局范围并依赖 window 来访问这些变量,或者您将落入 >评估魔鬼

这里有一个更好的主意:制裁剪体来存放你的书。一本书将如下所示:

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, ...];

突然之间,制作表格变得很容易!

// 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 变量。

// Loop over the book properties
for(var propertyName in headings) {
var headingName = headings[propertyName].text;
var textNode = document.createTextNode(headingName);

// [snipped]
}

关于JavaScript、表和数组。棘手的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10871900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com