var array = [
{
id: 1,
children:[{
id:2,
children:[]
}]
},
{
id:3,
children:[]
},
{
id:4,
children:[
{
id:5,
children:[
{
id:6,
children:[],
path:'aaa',
},
{
id:7,
path:'xxx',
children:[
{
id:187,
path:'zxxx',
}
]
}
]
}
]
}
]
//根据id找到对应的树节点
var returnedItem
var find = function(arr, id){
for(let i=0;i<arr.length;i++){
if( arr[i].id==id ){
returnedItem = arr[i]
break
}else if(arr[i].children && arr[i].children.length > 0){
find(arr[i].children, id); //递归调用
}
}
return returnedItem
// arr.forEach((item) => { //利用foreach循环遍历
// if(item.id==id)//判断递归结束条件
// {
// returnedItem = item;
// return item;
// }
// else if(item.children.length > 0) //判断chlidren是否有数据
// {
// find(item.children, id); //递归调用
// }
// })
}
// var item = find(array, 187);
console.log(find(array, 6))
// 获取到
// {
// id:6,
// children:[],
// path:'aaa',
// },
//找到树中最大的id
var tempid=0
function findmaxValue(arr){
for(let i=0;i<arr.length;i++){
if(arr[i].id>tempid){
tempid=arr[i].id
}
if(arr[i].children && arr[i].children.length > 0){
findmaxValue(arr[i].children);
}else{
// break
}
}
return tempid
}
findmaxValue(array)
//根据子节点,找到该子节点所有祖父的id
function treeFindPath(tree,func,path=[]){
if(!tree)return[]
for(const data of tree){
//记得修改这里,你想要的字段
path.push(data.id)
if(func(data)) return path
if(data.children){
const findChildren=treeFindPath(data.children,func,path)
if(findChildren.length)return findChildren
}
path.pop()
}
return[]
}
console.log(treeFindPath(array,data=>data['id']===187))
//[4, 5, 7, 187]
//根据子节点,向上找到该子节点整个祖父树
function findson_parents(vdata,key,value){
function treeFindPath(tree,func,path=[]){
if(!tree)return[]
for(const data of tree){
//记得修改这里,你想要的字段
path.push({
id:data.id,
path:data.path,
children:[]
})
if(func(data)) return path
if(data.children){
const findChildren=treeFindPath(data.children,func,path)
if(findChildren.length)return findChildren
}
path.pop()
}
return[]
}
var temarry=treeFindPath(vdata,data=>data[key]===value)
for(let i=0;i<temarry.length;i++){
if( temarry[i+1] ){
temarry[i].children.push(temarry[i+1])
}
}
return temarry[0]
}
console.log( findson_parents(array,'id',187) )
构建树

let source = [
{id:1,parentId:0,name:"一级菜单A",rank:1},
{id:2,parentId:0,name:"一级菜单B",rank:1},
{id:3,parentId:0,name:"一级菜单C",rank:1},
{id:4,parentId:1,name:"二级菜单A-A",rank:2},
{id:5,parentId:1,name:"二级菜单A-B",rank:2},
{id:6,parentId:2,name:"二级菜单B-A",rank:2},
{id:7,parentId:4,name:"三级菜单A-A-A",rank:3},
{id:8,parentId:7,name:"四级菜单A-A-A-A",rank:4},
{id:9,parentId:8,name:"五级菜单A-A-A-A-A",rank:5},
{id:10,parentId:9,name:"六级菜单A-A-A-A-A-A",rank:6},
{id:11,parentId:10,name:"七级菜单A-A-A-A-A-A-A",rank:7},
{id:12,parentId:11,name:"八级菜单A-A-A-A-A-A-A-A",rank:8},
{id:13,parentId:12,name:"九级菜单A-A-A-A-A-A-A-A-A",rank:9},
{id:14,parentId:13,name:"十级菜单A-A-A-A-A-A-A-A-A-A",rank:10},
];
function setTreeData(source){
let cloneData = JSON.parse(JSON.stringify(source)) // 对源数据深度克隆
return cloneData.filter(father=>{ // 循环所有项,并添加children属性
let branchArr = cloneData.filter(child=>father.id == child.parentId); // 返回每一项的子级数组
branchArr.length>0 ? father.children=branchArr : '' //给父级添加一个children属性,并赋值
return father.parentId==0; //返回第一层
});
}
console.log(setTreeData(source),1111111) // 树形数据
/* 封装函数 */
/* 字段名以字符串的形式传入 */
function treeData(source, id, parentId, children){
let cloneData = JSON.parse(JSON.stringify(source))
return cloneData.filter(father=>{
let branchArr = cloneData.filter(child => father[id] == child[parentId]);
branchArr.length>0 ? father[children] = branchArr : ''
return father[parentId] == 0
})
}
// 调用
console.log(treeData(source,'id','parentId','children'),22222222222)
//树转列表
let tree = [
{
id: 1,
name: 'text1',
parentId: 1,
children: [
{
id: 2,
name: 'text2',
parentId: 1,
children: [
{
id: 4,
name: 'text4',
parentId: 2
}
]
},
{
id: 3,
name: 'text3',
parentId: 1
}
]
}
]
function untieTree(tree, children_key='children') {
if(!!!tree || Object.prototype.toString.call(tree) !== '[object Array]' || tree.length<=0) return [];
const temp = [];
tree.forEach(item => {
temp.push(item, ...untieTree(item[children_key], children_key))
});
return temp;
}
//搜集指定层级的所有数据
var data5 = [{
i:'a1',
children: [{
i:'a2',
children: [{
i:'a3',
}, {
i:'a4',
}]
}, {
i:'a5',
children: [{
i:'a6',
}, {
i:'a7',
}]
}]
},
{
i:'a8',
children: [
{
i:'a9',
},
{
i:'a10',
}
]
}
];
var a1 = []
function dfs(list, level=1) {
list.forEach(item => {
item.level = level;
if(item.children) dfs(item.children, level+1)
if(item.level == 3){ //搜集第三级 的所有数据
var tem = JSON.parse(JSON.stringify(item))
delete tem.children
a1.push(tem)
}
})
return list;
}
dfs(data5)
console.log(a1)
[
{
"i": "a3",
"level": 3
},
{
"i": "a4",
"level": 3
},
{
"i": "a6",
"level": 3
},
{
"i": "a7",
"level": 3
}
]
//找到当前子节点的当前父节点
var arr0=
{
children:[
{
"children": [
{
"children": [
{
"children": [],
"jgdm": "460202",
"jglx": "3",
"jgmc": "三亚医院"
},
],
"jgdm": "460201",
"jglx": "2",
"jgmc": "海棠区"
},
],
"jgdm": "460200",
"jglx": "1",
"jgmc": "三亚市"
},
]
}
function findParentNode(data, id,attrName) {
//设置结果
let result;
if (!data) {
return;//如果data传空,直接返回
}
for (var i = 0; i < data.children.length; i++) {
let item = data.children[i];
if (item[attrName] == id) {
result = data;
return result; //这里是实际返回的值,你可以只返回当前对象,或者指定当前对象的某个字段。
} else if (item.children && item.children.length > 0) {
//如果有子集,则把子集作为参数重新执行本方法
result = findParentNode(item, id,attrName);
//关键,千万不要直接return本方法,不然即使没有返回值也会将返回return,导致最外层循环中断,直接返回undefined,要有返回值才return才对
if (result) {
return result;
}
}
}
//如果执行循环中都没有return,则在此return
return result;
}
var nodeEl = findParentNode(arr0, 460202,'jgdm'); //找到{"jgdm":460202}当前节点的父节点
console.log(nodeEl)
//树一共有多少层级,并返回最先找到最小的节点
function getMaxFloor (treeData) {
let max = 0
let childrenObj = null
function each (data, floor) {
data.forEach(e => {
// e.floor = floor //顺便附加层级
if (floor > max) {
max = floor
childrenObj = e
}
if (e.children && e.children.length > 0) {
each(e.children, floor + 1)
}
})
}
each(treeData,1)
return {max,childrenObj}
}
const treeData = [
{
title: 'parent 1',
key: '0-0',
children: [
{
title: 'parent 1-0',
key: '0-0-0',
disabled: true,
children: [
{
title: 'leaf2222',
key: '0-0-0-0',
disableCheckbox: true,
children: [
{
title: 'lea666',
key: '0-0-0-5',
disableCheckbox: true,
children: [
{
title: 'lea6776',
key: '0-0-0-5',
disableCheckbox: true,
},
],
},
],
},
],
}
],
}
];
//调用以上封装好的方法
const maxNum = getMaxFloor(treeData)
console.log(maxNum)