I created a nested array as I could so far but I feel like i did it wrong or something does not make sense. Could anyone please have a look and tell me if my array is the way to build nested array. All I want is to create rows under specific title, so I nested data and calling it with nested loops. Maybe theres is a simpler way of achiving it. Here is the code:
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>');
}
}
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: