树结构列表。这个技术点以前写过。是基于vue讲解的,但是貌似并没有解决痛点。如何实现最基本的原生JS?本文将对树结构列表的实现进行全面详细的介绍,从数据处理成树结构,到动态生成DOM节点渲染页面。确定原始数据结构原始数据需要按照如下结构进行定义。如果原始数据已经被后端处理成树状结构,则这一步可以省略。让数据=[{“id”:“1”,“name”:“1”,“fatherId”:“0”,},{“id”:“2”,“name”:“1-1”,"fatherId":"1"},{"id":"3","name":"1-2","fatherId":"1"},{"id":"4","name":"1-1-1","fatherId":"2"},{"id":"5","name":"1-1-2","fatherId":"2"},{"id":"6","name":"2","fatherId":"0"},{"id":"7","name":"1-2-1","fatherId":"3"}];使用map处理数据constmap={};constval=[];data.forEach((item)=>{map[item.id]=item;});data.forEach((item)=>{constparent=map[item.fatherId];if(parent){(parent.children||(parent.children=[])).push(item);}else{val.push(项目);}});打印出数组val看看。html结构
