我到目前为止创建了一个嵌套数组,但我觉得我做错了或者没有意义。任何人都可以看看,告诉我我的数组是否是构建嵌套数组的方法。我想要的只是在特定标题下创建行,所以我嵌套数据并使用嵌套循环调用它。也许theres是一种更简单的方法。这是代码:
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>');
}
}
答案 0 :(得分:3)
您正在使用的结构应该更像这样:
var data = [
{title:'Row Title 1', contents: [
{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”属性和“contents”属性的对象。你的循环看起来像这样:
for (var i=0, j=data.length; i < j; i++) {
if(data[i].title != null){
document.write('<b>'+data[i].title+'</b><br />');
}
for(var p=0, plen=data[i].contents.length; p < plen; p++){
document.write('<p style="background:#eee;">'+data[i].contents[p].leftCol+'</p>');
document.write('<p>'+data[i].contents[p].rightCol+'</p>');
}
}
答案 1 :(得分:1)
如果您想让代码更加强大,请遵循以下准则:
for (var i = 0, l = length; l--; i++)
,那么初始化for循环总是更好。 Nicholas C. Zakas详细解释了这种语法的原因。idata = data[i];
)。data[i].title != null
)。首先检查变量的类型。它速度较慢,但代码更容易理解和维护。试试帖子底部的typeOf
功能(例如typeOf(idata) === "Object"
)。===
代替==
和!==
代替!=
通常总是更好,因为它们不会执行可能导致意外结果的类型强制。< / LI>
.greyBackground { background-color: #EEEEEE; }
并将每个className
段落的leftCol
设置为greyBackground
。document.write
。它很慢,导致文档重排,并在页面下载时停止加载资源。使用JavaScript创建动态内容的最佳方法是使用document.createDocumentFragment
方法,我将在下面解释。document.write
或element.innerHTML
中使用字符串,则浏览器会解析字符串并将其转换为节点。因此使用该方法的速度较慢。这就是我编写JavaScript的方式:
var data = [
"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"
},
"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"
}
];
function typeOf(value) {
if (value === null) {
return "null";
} else if (typeof value === "undefined") {
return "undefined";
} else {
return Object.prototype.toString.call(value).slice(8, -1);
}
}
var element;
var fragment = document.createDocumentFragment();
var idata;
for (var i = 0, l = data.length; l--; i++) {
idata = data[i];
if (typeOf(idata) === "Object") {
element = document.createElement("p");
element.className = "greyBackground";
element.appendChild(document.createTextNode(idata.leftCol));
fragment.appendChild(element);
element = document.createElement("p");
element.appendChild(document.createTextNode(idata.rightCol));
fragment.appendChild(element);
} else {
element = document.createElement("b");
element.appendChild(document.createTextNode(idata));
fragment.appendChild(element);
element = document.createElement("br");
fragment.appendChild(element);
}
}
document.body.appendChild(fragment);
测试我和我的页面。很可能我的执行速度会更快。如果您有任何疑问,请随时问我。干杯! =)