/**
* 將扁平化數據(json)轉成jsonTree格式
* @param {[type]} data 扁平數據
* @param {[type]} config {id:'id', pid:'pid', children:'children'}
* id 數據裏的id string類型
* pid 數據裏的父id string類型
* children 生成結果中子節點的字段名 string類型
* @return {[type]} [description]
*/
export const getJsonTree = (data, config) => {
let id = config.id || 'id',
pid = config.pid || 'pid',
children = config.children || 'children',
idMap = [],
jsonTree = []
data.forEach(function(v) {
idMap[v[id]] = v
})
data.forEach(function(v) {
let parent = idMap[v[pid]]
if (parent) {
!parent[children] && (parent[children] = [])
parent[children].push(v)
} else {
jsonTree.push(v)
}
})
return jsonTree
}
將扁平化數據json轉成jsonTree格式
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.