现在有一组数据
const data =[
{
checked:'a',
children:[
{
checked:'b1',
children:[
{
checked:'c1',
storing:true,
children:[],
},
{
checked:'c2',
storing:true,
children:[],
},
{
checked:'c3',
storing:false,
children:[],
},
]
},
{
checked:'b2',
children:[
{
checked:'c1',
storing:true,
children:[],
},
{
checked:'c2',
storing:true,
children:[],
},
{
checked:'c3',
storing:false,
children:[],
},
]
},
{
checked:'b3',
children:[{
checked:'c1',
storing:true,
children:[],
},
{
checked:'c2',
storing:true,
children:[],
},
{
checked:'c3',
storing:false,
children:[],
},]
},
{
checked:'b4',
children:[{
checked:'c1',
storing:true,
children:[],
},
{
checked:'c2',
storing:true,
children:[],
},
{
checked:'c3',
storing:false,
children:[],
},]
},
]
}
]
要转化为一个一维数组类似
const arr = [{keyA:a1,keyB:b1,keyC:c1,key4:true},{keyA:a1,keyB:b1,keyC:c2,key4:true}
]
就是把所有可能都弄出来,这个示例应该是12种结果
目前我自己想的方法是
this.treeDataToCompressed(this.checkedList);
//this.checkedList 为处理前数组
treeDataToCompressed(source) {
source.forEach((cross) => {
this.finalResult.push(cross.checked);
if (cross.children && cross.children.length > 0) {
this.treeDataToCompressed(cross.children);
}
});
},
这样做第二次遍历的时候会直接跳过第一层 直接从第二次开始,有违初衷