score:3

Accepted answer

Here is a demo how it could be achieved with the pure javascript.

var createSublist = function(container, args) {
  var ul = document.createElement('ul');
  
  for(var j = 0; j < args.length; j++) {
    var row = args[j];
    
    var li = document.createElement('li');
    li.innerText = row.text;
    
    var nodes = row.nodes;
    if(nodes && nodes.length) {
      createSublist(li, nodes);
    }
    
    ul.appendChild(li);
  }
  
  container.appendChild(ul);
};

var data =[  
   {  
      "text":"France",
      "nodes":[  
         {  
            "text":"Bread"
         },
         {  
            "text":"Nocco"     
         }
      ],

   },
   {  
      "text":"Italy",
      "nodes":[  
         {  
            "text":"Pizza"
         },
         {  
            "text":"Wine",
            "nodes": [
              { 
              	"text": "Red"
              },
              { 
              	"text":"White"
              }
            ]
         }
      ]
   }
];

var container = document.getElementsByClassName('container')[0];  
if(container) 
{ 
  createSublist(container, data);
} 
else 
{
  console.log('Container has not been found'); 
}
  
<div class="container">
</div>

score:2

JavaScript ES6 and a small recursive function

/**
 * Convert Tree structure to UL>LI and append to Element
 * @syntax getTree(treeArray [, TargetElement [, onLICreatedCallback ]])
 *
 * @param {Array} tree Tree array of nodes
 * @param {Element} el HTMLElement to insert into
 * @param {function} cb Callback function called on every LI creation
 */

const treeToHTML = (tree, el, cb) => el.append(
  tree.reduce((ul, n) => {
    const li = document.createElement("li");

    if (cb) cb.call(li, n);
    if (n.nodes?.length) treeToHTML(n.nodes, li, cb);

    ul.append(li);
    return ul;
  }, document.createElement("ul"))
);





// DEMO TIME:

const myTree = [{
  "id": 1,
  "parent_id": 0,
  "name": "Item1",
  "nodes": [{
    "id": 2,
    "parent_id": 1,
    "name": "Item2"
  }]
}, {
  "id": 4,
  "parent_id": 0,
  "name": "Item4",
  "nodes": [{
      "id": 9,
      "parent_id": 4,
      "name": "Item9"
    }, {
      "id": 5,
      "parent_id": 4,
      "name": "Item5",
      "nodes": [{
        "id": 3,
        "parent_id": 5,
        "name": "Item3"
      }]
    }
  ]
}];

treeToHTML(myTree, document.querySelector("#demo"), function(node) {
  this.textContent = `${node.parent_id} ${node.id} ${node.name}`
});
<div id="demo"></div>


Related Query

More Query from same tag