I'm taking an intro JS course at my college and I have to build a <ul> with a nested array through JS. I've already built an <ol> with a regular array for the first part of the assignment. This is what I did:
window.onload = function(){
fruits.sort();
var ol = "<ol>"
for(var i = 0; i < fruits.length; i++){
ol+= "<li>" + fruits[i] + "</li>"
}ol += "</ol>"
var myContainer = document.querySelector("#olContainer").innerHTML = ol;
};
For this array:
var fruits = [ "Apples","Oranges","Pears","Grapes","Pineapples","Mangos" ];
and the current nested array I'm working with, is this:
var directory = [
{type: "file", name: "file1.txt"},
{type: "file", name: "file2.txt"},
{type: "directory", name: "HTML Files", files: [{type: "file", name: "file1.html"},{type: "file", name: "file2.html"}]},
{type: "file", name: "file3.txt"},
{type: "directory", name: "JavaScript Files", files: [{type: "file", name: "file1.js"},{type: "file", name: "file2.js"},{type: "file", name: "file3.js"}]}
];
I am thinking that using a switch statement would be better but I don't know how to iterate through each line of code and extracting the nested lists to indent them as a sublist. I am required to build upon my previous JS function for the <ol> list
<ol>, you can put it into a function which accepts an argument so you can change theoltoul. It is also preferable to use createElement and appendChild as manipulating innerHTML is hard on the browser memory and garbage collector.directory.forEach(item=>(console.log("name:", item.name)))