Create HTML List From Javascript Array/Object (Simple Examples)

Welcome to a tutorial on how to create an HTML list from a Javascript array or object. Need to display an array of data nicely?

To create an HTML list from an array in Javascript:

  1. Create the HTML list var ol = document.createElement("ol");
  2. Loop through the array and append list items for (let i of ARRAY) { let li = document.createElement("li"); li.innerHTML = i; ol.appendChild(li); }
  3. Append the list to where you want document.getElementById("TARGET").appendChild(ol);

That covers the basics, read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Create List Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

ARRAY/OBJECT TO LIST

All right, let us now get into the examples of generating an HTML list from an array or object.

 

1) CREATE & APPEND HTML ELEMENTS

1-create-element.html
<!-- (A) DIV CONTAINER -->
<div id="demoA"></div>
 
<!-- (B) JAVASCRIPT -->
<script>
// (B1) DUMMY ARRAY
var arr = ["Apple", "Banana", "Cherry"];

// (B2) CREATE LIST
var list = document.createElement("ol");
for (let i of arr) {
  let item = document.createElement("li");
  item.innerHTML = i;
  list.appendChild(item);
}

// (B3) APPEND LIST TO CONTAINER
document.getElementById("demoA").appendChild(list);
</script>

This is the “full version” of the snippet in the introduction – It should be pretty straightforward and self-explanatory.

  • Create the list.
  • Loop and append the list items.
  • Then attach the entire list to wherever you want.

 

 

2) MANUAL HTML STRING

2-create-html.html
<!-- (A) DIV CONTAINER -->
<div id="demoB"></div>
 
<!-- (B) JAVASCRIPT -->
<script>
// (B1) DUMMY ARRAY
var arr = ["Apple", "Banana", "Cherry"];

// (B2) CREATE LIST
var list = "<ol>";
for (let i of arr) {
  list += `<li>${i}</li>`;
}
list += "</ol>";

// (B3) APPEND LIST TO CONTAINER
document.getElementById("demoB").innerHTML = list;
</script>

If the above “object-oriented way” of creating HTML elements is not your cup of tea, here is the alternative – We manually generate a string of HTML list, then attach it to the container.

 

 

3) NESTED OBJECT

3-object.php
<!-- (A) DIV CONTAINER -->
<div id="demoC"></div>

<!-- (B) JAVASCRIPT -->
<script>
// (B1) DUMMY OBJECT
var obj = {
  "Fruits" : ["Apple", "Banana", "Cherry"],
  "Vegetables" : ["Celery", "Cabbage", "Carrot"]
};

// (B2) CREATE LIST
var list = document.createElement("ul");
for (let i in obj) {
  // SUB-LIST HEADING
  let item = document.createElement("li");
  let subitem = document.createElement("strong");
  subitem.innerHTML = i;
  list.appendChild(item);
  item.appendChild(subitem);

  // SUB-LIST ITEMS
  let sublist = document.createElement("ul");
  item.appendChild(sublist);
  for (let j of obj[i]) {
    subitem = document.createElement("li");
    subitem.innerHTML = j;
    sublist.appendChild(subitem);
  }
}

// (B3) APPEND LIST TO CONTAINER
document.getElementById("demoC").appendChild(list);
</script>

Finally, how do we deal with nested objects? This example is seemingly intimidating at first, but keep calm and look through slowly – We are pretty much doing the same thing, looping through the nested object and creating the necessary HTML elements.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Create HTML List From Javascript Array (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.