js多维数组递归,并且添加层级
var array=[{
id:"0", title:"目录", children:[{ id:"1", title:"我的音乐", children:[{ id:"2", title:"刘若英", children:[{ id:"3", title:"后来" },{ id:"3", title:"我们没有在一起" },{ id:"3", title:"成全" }] },{ id:"2", title:"周杰伦" },{ id:"2", title:"蔡依林" }] },{ title:"我的书籍", children:[{ title:"javascript" },{ title:"html css" }] }] }]; var items=[]; function render(arr,level){ var level=level||0; level++; var i= 0,len=arr.length; var str="<ul style='padding-left:20px'>" for(i=0;i<len;i++){ str+="<li>"+level+"、"+arr[i].title; if(arr[i].children&&arr[i].children.length>0){ str+=render(arr[i].children,level); items.push([{"title":arr[i].title,"id":arr[i].id}]); } str+="</li>"; } str+="</ul>"; return str; } render(array,0); document.body.innerHTML=render(array); console.log(items);