I'm working on a chrome extension and need to generate nested DOM-elements based on categories. These categories are saved as strings in an array. The hierarchy level of sub-categories is shown through paths.
Here is an simple example
category array:
let categories = [a, a/b, b, b/c/d];
I want to generate nested div elements like that:
<div id="a">
<div id="b"></div>
</div>
<div id="b">
<div id="c">
<div id="d"></div>
</div>
</div>
With my current status I only create the unique top level div's and don't know how to generate the unique sub-categories in the top level div's as well.
categories.forEach(function (i) {
let splitedCategory = i.split('/');
let parentID = String(splitedCategory[0]);
let parentElement = document.getElementById(parentID);
if(!containerElement.contains(parentElement)) {
containerElement.innerHTML += '<div id="' + i + '"></div>';
}
});