What I'm trying to output with JS:
<ul>
<li>title1
<ul>
<li>image1</li>
<li>image2</li>
<li>image3</li>
</ul>
</li>
<li>title2
<ul>
<li>image1</li>
<li>image2</li>
<li>image3</li>
</ul>
</li>
...and so forth...
</ul>
JSON:
var data = [
{
title: "title1",
image:["image1", "image2", "image3"]
},
{
title: "title2",
image:["image1", "image2", "image3"]
},
{
title: "title3",
image:["image1", "image2", "image3"]
},
{
title: "title4",
image:["image1", "image2", "image3"]
}
];
My JS
for(var i=0; i < data.length; i++) {
var item = data[i];
var obj = {
title:item.title,
image:item.image
};
var theimages;
var html = '';
for(j=0; j < item.image.length; j++) {
theimages = '<li>' + item.image[j] + '</li>';
}
html += '<li>' + item.title + '<ul>';
html += theimages;
html += '</ul></li>';
}
console.log(html);
Can someone explain how do I get the value of the inner for
loop
and combine it with the outer for
loop
so that I end up with output I'm trying to achieve. currently I end up with this:
<li>title4
<ul>
<li>image3</li>
</ul>
</li>
for
loop, you should use+=
as intheimages += ...
so it always adds to the end of the string, instead of replacing it. – Puuskis Jun 26 '13 at 9:41