【js】id,pid扁平数据转换为树形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* 树形数据转换
* @param {*} data
* @param {*} id
* @param {*} pid
*/
export function treeDataTranslate (data, id = 'id', pid = 'pid') {
var res = []
var temp = {}
for (var i = 0; i < data.length; i++) {
temp[data[i][id]] = data[i]
}
for (var k = 0; k < data.length; k++) {
if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) {
res.push(data[k])
continue
}
if (!temp[data[k][pid]]['children']) {
temp[data[k][pid]]['children'] = []
}
temp[data[k][pid]]['children'].push(data[k])
data[k]['_level'] = (temp[data[k][pid]]._level || 0) + 1
}
return res
}