给定一个数组,数组记录了当前数据的ID,和后面紧邻的数据ID。对该数据进行分组,实现以下功能:
功能描述:
对数组进行分类,
如果当前数据没有被其他数据的next指向,则为一个数组的第一个数据,
如果当前数据有被其他数据的next指向,则排在next指向的后面,
其他以此类推。
const data = [
{ id: 1, next: 2 },
{ id: 3, next: 4 },
{ id: 4, next: 5 },
{ id: 5, next: 6 },
{ id: 6, next: 7 },
{ id: 7, next: 8 },
{ id: 8, next: 9 },
{ id: 2, next: 10 },
{ id: 20, next: 30 },
{ id: 30, next: 40 },
{ id: 100, next: 78 }
]
getLinks(data)
=>
[
[{ id: 1, next: 2 }, { id: 2, next: 10 }],
[{ id: 3, next: 4 }, { id: 4, next: 5 }, { id: 5, next: 6 }, { id: 6, next: 7 }, { id: 7, next: 8 }, { id: 8, next: 9 }]
[{ id: 20, next: 30 }, { id: 30, next: 40 }],
[{ id: 100, next: 78 }]
]
提供思路,哈希表 新建 {"id":"newxt" } 的 js object 。{1:2,2:10,3:4...}
循环取出来就行