In javascript, I have an array of objects that represents an arbitrarily deep list…
data =
[
{ title, depth },
{ title, depth },
{ title, depth },
{ title, depth },
]
…where depth is how deep in the list the element is.
I want to convert this data into html.
For example:
[
{ title: "one", depth : 1 },
{ title: "two", depth : 1 },
{ title: "three", depth : 2 },
{ title: "four", depth : 3 },
{ title: "five", depth : 1 },
]
becomes…
<ul>
<li><p>one</p></li>
<li>
<p>two</p>
<ul>
<li>
<p>three</p>
<ul>
<li><p>four</p></li>
</ul>
</li>
</ul>
</li>
<li><p>five</p></li>
</ul>
What is the simplest way to do this, using jQuery?
Thanks
Here is one way you could do it:
I admit, I just wanted to name something listify. Here is a jsFiddle. You would call it like so: