/**
*
* @param {Array} arrs 树形数据
* @param {string} childs 树形数据子数据的属性名,常用'children'
* @param {Array} attrArr 需要提取的公共属性数组(默认是除了childs的全部属性)
* @returns
*/
function extractTree(arrs,childs,attrArr){
let attrList = [];
if(!Array.isArray(arrs)&&!arrs.length)return [];
if(typeof childs !== 'string')return [];
if(!Array.isArray(attrArr)||Array.isArray(attrArr)&&!attrArr.length){
attrList = Object.keys(arrs[0]);
attrList.splice(attrList.indexOf(childs), 1);
}else{
attrList = attrArr;
}W
let list = [];
const getObj = (arr)=>{
arr.forEach(function(row){
let obj = {};
attrList.forEach(item=>{
obj[item] = row[item];
});
list.push(obj);
if(row[childs]){
getObj(row[childs]);
}
})
return list;
}
return getObj(arrs);
}
const arr = extractTree(list,'childs',['id'])