Nodejs版本的无限分类 树形数据 格式化
Nodejs版本的无限分类 树形数据 格式化
function genTree(list) {
var temptree = [], tree = [], items = [];
for (var i in list) {
if (!temptree[list[i].did]) {
var trow = {
id: 'z' + list[i].did,
did: list[i].did,
fid: list[i].fid,
text: list[i].text,
iconCls: 'im_role',
children: []
};
temptree[list[i].did] = trow;
items.push(trow);
}
if (list[i].uid > 0) {
temptree[list[i].did]['children'].push({
id: list[i].uid,
text: list[i].realname,
iconCls: 'im_user'
});
}
}
for (var j in items) {
if (temptree[items[j].fid]) {
temptree[items[j].fid]['children'].push(temptree[items[j].did]);
} else {
tree.push(temptree[items[j].did]);
}
}
temptree = null;
items = null;
return tree;
}
var list=[//目录
{did:1,fid:0,text:‘1111’},
{did:2,fid:1,text:‘2222’},
{did:3,fid:1,text:‘3333’},
{did:4,fid:1,text:‘4444’},
{did:5,fid:3,text:‘5555’},
{did:6,fid:4,text:‘6666’},
{did:7,fid:5,text:‘7777’},
{did:8,fid:3,text:‘8888’},
{did:9,fid:4,text:‘9999’},
//目录下面的人
{uid:1,did:2,realname:'r111'},
{uid:2,did:9,realname:'r222'},
{uid:3,did:2,realname:'r333'},
{uid:4,did:6,realname:'r444'},
{uid:5,did:7,realname:'r555'},
{uid:6,did:8,realname:'r666'},
{uid:7,did:9,realname:'r777'},
//或者在同一条记录里面,数据需要先排序保证目录在前面
{did:10,fid:1,text:'1010',uid:10,realname:'r1010'},
{did:11,fid:10,text:'1111',uid:11,realname:'r1111'},
{did:12,fid:11,text:'1212',uid:12,realname:'r1212'},
{did:13,fid:12,text:'1313',uid:13,realname:'r1313'}
];
var tree=genTree(list);
console.log(tree);
/*
[
{
“id”: “z1”,
“did”: 1,
“fid”: 0,
“text”: “1111”,
“iconCls”: “im_role”,
“children”: [
{
“id”: “z2”,
“did”: 2,
“fid”: 1,
“text”: “2222”,
“iconCls”: “im_role”,
“children”: [
{
“id”: 1,
“text”: “r111”,
“iconCls”: “im_user”
},
{
“id”: 3,
“text”: “r333”,
“iconCls”: “im_user”
}
]
},
{
“id”: “z3”,
“did”: 3,
“fid”: 1,
“text”: “3333”,
“iconCls”: “im_role”,
“children”: [
{
“id”: “z5”,
“did”: 5,
“fid”: 3,
“text”: “5555”,
“iconCls”: “im_role”,
“children”: [
{
“id”: “z7”,
“did”: 7,
“fid”: 5,
“text”: “7777”,
“iconCls”: “im_role”,
“children”: [
{
“id”: 5,
“text”: “r555”,
“iconCls”: “im_user”
}
]
}
]
},
{
“id”: “z8”,
“did”: 8,
“fid”: 3,
“text”: “8888”,
“iconCls”: “im_role”,
“children”: [
{
“id”: 6,
“text”: “r666”,
“iconCls”: “im_user”
}
]
}
]
},
{
“id”: “z4”,
“did”: 4,
“fid”: 1,
“text”: “4444”,
“iconCls”: “im_role”,
“children”: [
{
“id”: “z6”,
“did”: 6,
“fid”: 4,
“text”: “6666”,
“iconCls”: “im_role”,
“children”: [
{
“id”: 4,
“text”: “r444”,
“iconCls”: “im_user”
}
]
},
{
“id”: “z9”,
“did”: 9,
“fid”: 4,
“text”: “9999”,
“iconCls”: “im_role”,
“children”: [
{
“id”: 2,
“text”: “r222”,
“iconCls”: “im_user”
},
{
“id”: 7,
“text”: “r777”,
“iconCls”: “im_user”
}
]
}
]
},
{
“id”: “z10”,
“did”: 10,
“fid”: 1,
“text”: “1010”,
“iconCls”: “im_role”,
“children”: [
{
“id”: 10,
“text”: “r1010”,
“iconCls”: “im_user”
},
{
“id”: “z11”,
“did”: 11,
“fid”: 10,
“text”: “1111”,
“iconCls”: “im_role”,
“children”: [
{
“id”: 11,
“text”: “r1111”,
“iconCls”: “im_user”
},
{
“id”: “z12”,
“did”: 12,
“fid”: 11,
“text”: “1212”,
“iconCls”: “im_role”,
“children”: [
{
“id”: 12,
“text”: “r1212”,
“iconCls”: “im_user”
},
{
“id”: “z13”,
“did”: 13,
“fid”: 12,
“text”: “1313”,
“iconCls”: “im_role”,
“children”: [
{
“id”: 13,
“text”: “r1313”,
“iconCls”: “im_user”
}
]
}
]
}
]
}
]
}
]
}
]*/
今天弄了一天,不知道有什么错误没有,或者更优化的方法
Node.js 版本的无限分类树形数据格式化
问题描述
在处理树形结构的数据时,我们经常遇到一个挑战,那就是如何将扁平化的数据转换成嵌套的树形结构。例如,给定一组扁平化的数据,我们需要将其转换为具有层级关系的树形结构。
示例数据
假设我们有以下扁平化数据:
var list = [
{did:1,fid:0,text:'1111'},
{did:2,fid:1,text:'2222'},
{did:3,fid:1,text:'3333'},
{did:4,fid:1,text:'4444'},
{did:5,fid:3,text:'5555'},
{did:6,fid:4,text:'6666'},
{did:7,fid:5,text:'7777'},
{did:8,fid:3,text:'8888'},
{did:9,fid:4,text:'9999'},
// 目录下面的人
{uid:1,did:2,realname:'r111'},
{uid:2,did:9,realname:'r222'},
{uid:3,did:2,realname:'r333'},
{uid:4,did:6,realname:'r444'},
{uid:5,did:7,realname:'r555'},
{uid:6,did:8,realname:'r666'},
{uid:7,did:9,realname:'r777'},
// 或者在同一条记录里面,数据需要先排序保证目录在前面
{did:10,fid:1,text:'1010',uid:10,realname:'r1010'},
{did:11,fid:10,text:'1111',uid:11,realname:'r1111'},
{did:12,fid:11,text:'1212',uid:12,realname:'r1212'},
{did:13,fid:12,text:'1313',uid:13,realname:'r1313'}
];
解决方案
我们可以编写一个函数 genTree
来将上述扁平化数据转换为树形结构:
function genTree(list) {
var temptree = {}, tree = [], items = [];
// 遍历扁平化数据
for (var i = 0; i < list.length; i++) {
var item = list[i];
// 如果当前项是一个目录
if (item.uid === undefined) {
var trow = {
id: 'z' + item.did,
did: item.did,
fid: item.fid,
text: item.text,
iconCls: 'im_role',
children: []
};
temptree[item.did] = trow;
items.push(trow);
}
// 如果当前项是一个用户
if (item.uid !== undefined) {
if (!temptree[item.did]) {
temptree[item.did] = {
id: 'z' + item.did,
did: item.did,
fid: item.fid,
text: item.text,
iconCls: 'im_role',
children: []
};
items.push(temptree[item.did]);
}
temptree[item.did]['children'].push({
id: item.uid,
text: item.realname,
iconCls: 'im_user'
});
}
}
// 构建树形结构
for (var j = 0; j < items.length; j++) {
var item = items[j];
if (temptree[item.fid]) {
temptree[item.fid]['children'].push(item);
} else {
tree.push(item);
}
}
return tree;
}
var tree = genTree(list);
console.log(JSON.stringify(tree, null, 2));
输出结果
上述代码将输出以下树形结构:
[
{
"id": "z1",
"did": 1,
"fid": 0,
"text": "1111",
"iconCls": "im_role",
"children": [
{
"id": "z2",
"did": 2,
"fid": 1,
"text": "2222",
"iconCls": "im_role",
"children": [
{
"id": 1,
"text": "r111",
"iconCls": "im_user"
},
{
"id": 3,
"text": "r333",
"iconCls": "im_user"
}
]
},
{
"id": "z3",
"did": 3,
"fid": 1,
"text": "3333",
"iconCls": "im_role",
"children": [
{
"id": "z5",
"did": 5,
"fid": 3,
"text": "5555",
"iconCls": "im_role",
"children": [
{
"id": "z7",
"did": 7,
"fid": 5,
"text": "7777",
"iconCls": "im_role",
"children": [
{
"id": 5,
"text": "r555",
"iconCls": "im_user"
}
]
}
]
},
{
"id": "z8",
"did": 8,
"fid": 3,
"text": "8888",
"iconCls": "im_role",
"children": [
{
"id": 6,
"text": "r666",
"iconCls": "im_user"
}
]
}
]
},
{
"id": "z4",
"did": 4,
"fid": 1,
"text": "4444",
"iconCls": "im_role",
"children": [
{
"id": "z6",
"did": 6,
"fid": 4,
"text": "6666",
"iconCls": "im_role",
"children": [
{
"id": 4,
"text": "r444",
"iconCls": "im_user"
}
]
},
{
"id": "z9",
"did": 9,
"fid": 4,
"text": "9999",
"iconCls": "im_role",
"children": [
{
"id": 2,
"text": "r222",
"iconCls": "im_user"
},
{
"id": 7,
"text": "r777",
"iconCls": "im_user"
}
]
}
]
},
{
"id": "z10",
"did": 10,
"fid": 1,
"text": "1010",
"iconCls": "im_role",
"children": [
{
"id": 10,
"text": "r1010",
"iconCls": "im_user"
},
{
"id": "z11",
"did": 11,
"fid": 10,
"text": "1111",
"iconCls": "im_role",
"children": [
{
"id": 11,
"text": "r1111",
"iconCls": "im_user"
},
{
"id": "z12",
"did": 12,
"fid": 11,
"text": "1212",
"iconCls": "im_role",
"children": [
{
"id": 12,
"text": "r1212",
"iconCls": "im_user"
},
{
"id": "z13",
"did": 13,
"fid": 12,
"text": "1313",
"iconCls": "im_role",
"children": [
{
"id": 13,
"text": "r1313",
"iconCls": "im_user"
}
]
}
]
}
]
}
]
}
]
}
]
总结
通过上述方法,我们可以将扁平化的数据转换为嵌套的树形结构。这种方法可以处理复杂的多层级关系,并且代码简洁易懂。
这个问题的需求是将一个扁平化的无限层级分类数据结构转换成树形结构。下面是改进后的代码示例:
function genTree(list) {
const map = {};
const roots = [];
list.forEach(item => {
map[item.did] = { ...item, children: [] };
if (item.fid === 0) {
roots.push(map[item.did]);
} else {
if (!map[item.fid]) {
map[item.fid] = { children: [] };
}
map[item.fid].children.push(map[item.did]);
}
});
list.filter(item => item.uid > 0).forEach(item => {
if (map[item.did].children) {
map[item.did].children.push({ id: item.uid, text: item.realname, iconCls: 'im_user' });
}
});
return roots;
}
var list = [
// 目录
{ did: 1, fid: 0, text: '1111' },
{ did: 2, fid: 1, text: '2222' },
{ did: 3, fid: 1, text: '3333' },
{ did: 4, fid: 1, text: '4444' },
{ did: 5, fid: 3, text: '5555' },
{ did: 6, fid: 4, text: '6666' },
{ did: 7, fid: 5, text: '7777' },
{ did: 8, fid: 3, text: '8888' },
{ did: 9, fid: 4, text: '9999' },
// 目录下面的人
{ uid: 1, did: 2, realname: 'r111' },
{ uid: 2, did: 9, realname: 'r222' },
{ uid: 3, did: 2, realname: 'r333' },
{ uid: 4, did: 6, realname: 'r444' },
{ uid: 5, did: 7, realname: 'r555' },
{ uid: 6, did: 8, realname: 'r666' },
{ uid: 7, did: 9, realname: 'r777' },
// 或者在同一条记录里面,数据需要先排序保证目录在前面
{ did: 10, fid: 1, text: '1010', uid: 10, realname: 'r1010' },
{ did: 11, fid: 10, text: '1111', uid: 11, realname: 'r1111' },
{ did: 12, fid: 11, text: '1212', uid: 12, realname: 'r1212' },
{ did: 13, fid: 12, text: '1313', uid: 13, realname: 'r1313' }
];
var tree = genTree(list);
console.log(JSON.stringify(tree, null, 2));
这段代码通过创建一个映射表(map
)来存储每个节点,并使用 roots
来保存根节点。然后遍历列表,根据 fid
将节点添加到父节点的 children
数组中。最后处理带有 uid
的项,将其添加到相应的父节点中。这样可以确保生成的树形结构正确无误。