/**
* 将id、parentId这种JSON数组的数据格式转换为树节点格式
* @param {Array} arr
* @param {String} id
* @param {String} pid
* @return {Array}
*/
function arrayToTree(arr, id, pid) {
let data = JSON.parse(JSON.stringify(arr));
if (!data || !data.length) return [];
let targetData = []; //存储数据的容器(返回)
let records = {};
let itemLength = data.length; //数据集合的个数
for (let i = 0; i < itemLength; i++){
let o = data[i];
records[o[id]] = o;
}
for (let i = 0; i < itemLength; i++) {
let currentData = data[i];
let parentData = records[currentData[pid]];
if (!parentData) {
targetData.push(currentData);
continue;
}
parentData.children = parentData.children || [];
parentData.children.push(currentData);
}
return targetData;
}
/**
* 将数组转换为树结构数据
*
* @export
* @param {Array} array
* @param {Object} parent 父节点
* @param {Array} tree
* @return {Array}
*/
function array2tree(array, parent = { id: 0 }, tree = []) {
let treeData = tree
const children = _.filter(array, function(child) {
return child.parentId === parent.id
})
if (!_.isEmpty(children)) {
if (parent.id === 0) {
treeData = children
} else {
parent['children'] = children
}
_.each(children, function(child) {
array2tree(array, child)
})
}
return treeData
}