带有嵌套循环的 JavaScript 嵌套数组
到目前为止,我尽我所能创建了一个嵌套数组,但我觉得我做错了或者有些事情没有意义。谁能看一下并告诉我我的数组是否是构建嵌套数组的方法。我想要的只是在特定标题下创建行,因此我嵌套数据并使用嵌套循环调用它。也许有一种更简单的方法来实现它。这是代码:
var data = [
{title:'Row Title 1'},
[{leftCol:'Some text for left column',rightCol:'Some text for right column'},
{leftCol:'Some text for left column',rightCol:'Some text for right column'},
{leftCol:'Some text for left column',rightCol:'Some text for right column'}],
{title:'Row Title 2'},
[{leftCol:'Some text for left column',rightCol:'Some text for right column'},
{leftCol:'Some text for left column',rightCol:'Some text for right column'},
{leftCol:'Some text for left column',rightCol:'Some text for right column'}]
];
for (var i=0, j=data.length; i < j; i++) {
if(data[i].title != null){
document.write('<b>'+data[i].title+'</b><br />');
}
for(p=0,plen=data[i].length; p<plen;p++){
document.write('<p style="background:#eee;">'+data[i][p].leftCol+'</p>');
document.write('<p>'+data[i][p].rightCol+'</p>');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的结构应该更像这样:
这样,每一行都是一个具有“标题”属性和“内容”属性的对象。你的循环将如下所示:
The structure you're using should be more like this:
That way, each row is an object with a "title" attribute and a "contents" attribute. Your loop would then look like this:
如果您想让代码更加健壮,请遵循以下准则:
for (var i = 0, l = length; l--; i++). Nicholas C. Zakas 更详细地解释了这种语法的原因。
idata = data[i];
)。data[i].title != null
)。首先检查变量的类型。它速度较慢,但代码更易于理解和维护。尝试帖子底部的typeOf
函数(例如typeOf(idata) === "Object"
)。===
而不是==
和!==
而不是!=
总是更好,因为它们不执行可能导致意外结果的类型强制。.greyBackground { background-color: #EEEEEE; }
并将每个leftCol
段落的className
设置为greyBackground
。document.write
。它很慢,会导致文档回流,并在页面下载时停止加载资源。使用 JavaScript 创建动态内容的最佳方法是使用document.createDocumentFragment
方法,我将在下面解释。document.write
或element.innerHTML
中使用字符串,则浏览器会解析该字符串并将其转换为节点。因此使用该方法速度较慢。这就是我编写 JavaScript 的方式:
测试我的页面和你的页面。很可能我的执行速度会更快。如果您有任何疑问,请随时问我。干杯! =)
If you want to make your code more robust follow these guidelines:
for (var i = 0, l = length; l--; i++)
. The reason for this syntax is explained in fuller detail by Nicholas C. Zakas.idata = data[i];
).data[i].title != null
). Check for the type of the variable first. It's slower, but the code is easier to understand and maintain. Try thetypeOf
function at the bottom of the post (e.g.typeOf(idata) === "Object"
).===
instead of==
and!==
instead of!=
because they don't perform type coercion which might lead to unexpected results..greyBackground { background-color: #EEEEEE; }
and set theclassName
of eachleftCol
paragraph togreyBackground
.document.write
. It's slow, causes reflow of the document, and halts loading assets while the page is downloading. The best way to create dynamic content using JavaScript is to use thedocument.createDocumentFragment
method as I'll explain below.document.write
orelement.innerHTML
then the browser parses the string and converts it into the nodes anyway. Thus using that method is slower.This is how I would have written your JavaScript:
Test my page and yours. In all probability mine will execute faster. If you have any doubts feel free to ask me. Cheers! =)