【发布时间】:2021-04-04 11:22:27
【问题描述】:
首先,我有一组使用simple-crawler 库抓取的网址。
接收到的数据就是我要转换成树形结构或者文件夹结构的。 我在这里使用react-tabulator,因为我想调整表格列的大小。 现在与普通表一起,我想要嵌套的文件夹视图结构。
//input data
const urls = [
{ id: 1, address: 'https://happy.com' },
{ id: 2, address: 'https://happy.com/about' },
{ id: 3, address: 'https://happy.com/contact' },
{ id: 4, address: 'https://happy.com/contact/office' },
{ id: 5, address: 'https://happy.com/contact/home' },
{ id: 6, address: 'https://happy.com/projects' },
];
//output data
tableDataNested = [
{ id: 1, address: 'https://happy.com',
_children:[
{ id: 2, address: 'https://happy.com/about', _children:[] },
{ id: 3, address: 'https://happy.com/contact',
_children:[
{ id: 4, address: 'https://happy.com/contact/office', _children:[] },
{ id: 5, address: 'https://happy.com/contact/home', _children:[] },
]
},
{ id: 6, address: 'https://happy.com/projects', _children:[] },
]
}
];
虽然我看到了 1-2 篇类似这个概念的帖子,但我不确定纯 JS 的做法,或者也可能使用一些不错的库。 有什么见解吗?
【问题讨论】:
标签: javascript arrays reactjs html-table