猿问

怎么遍历这样树形结构的数组

像这样树形结构的数组,我想给所有的name换成title,怎么遍历呢。数组的层次不定,可能有的里面没children。
consttreeData=[{
title:'0-0',
key:'0-0',
children:[{
title:'0-0-0',
key:'0-0-0',
children:[
{title:'0-0-0-0',key:'0-0-0-0'},
{title:'0-0-0-1',key:'0-0-0-1'},
{title:'0-0-0-2',key:'0-0-0-2'},
],
},{
title:'0-0-1',
key:'0-0-1',
children:[
{title:'0-0-1-0',key:'0-0-1-0'},
{title:'0-0-1-1',key:'0-0-1-1'},
{title:'0-0-1-2',key:'0-0-1-2'},
],
},{
title:'0-0-2',
key:'0-0-2',
}],
},{
title:'0-1',
key:'0-1',
children:[
{title:'0-1-0-0',key:'0-1-0-0'},
{title:'0-1-0-1',key:'0-1-0-1'},
{title:'0-1-0-2',key:'0-1-0-2'},
],
},{
title:'0-2',
key:'0-2',
children:[]
}];
SMILET
浏览 1278回答 2
2回答

LEATH

递归遍历functionsetName(obj){for(variinobj){obj[i].name=obj[i].title;if(obj[i].chilren){setName(obj[i].children)}}console.log(obj);}setName(treeData)

慕的地8271018

functiontreedata(a){letd=a.map(r=>{if(r.children){return{'name':r.title,'key':r.key,'children':treedata(r.children)}}else{return{'name':r.title,'key':r.key}}})returnd}console.log(treedata(treeData))
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答