下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
// An highlighted block
// 遍历 this.filePath
this.filePath.forEach((item) => {
console.log(item, 't')
// 处理item 这里item this.filePath[i]
function toTreeData (str) {
var str2 = str.split('/')
console.log(str2, 'a')
console.log(str2.length, 'b')
for (var i = 0; i < str2.length; i++) {
console.log(str2[0], '0c')
console.log(str2[1], '1c')
console.log(str2[2], '2c')
var arrTable = {
id: '1',
title: str2[0],
parentId: '',
children: [
{
id: '2',
title: str2[1],
parentId: '1',
children: [{
id: '3',
title: str2[2],
parentId: '2'
}]
}
]
}
console.log(arrTable, 'd')
return arrTable
}
}
const dataRecive = toTreeData(item)
// item处理结束
// 多个item塞到对象里
treeData.push(dataRecive)
})
console.log(treeData, 'p')
运行
算法思想
.循环每一项,在循环里写一个函数,对循环里的每一项进行处理
难点
1.数组里的item 此处是 字符串,通过 split(’/’)方法截取出来,得到的是字符组成的数组
2.函数方法 调用之后 要用一个参数接收