1. 父子关系数据(一维数组)转换为树型结构数据
1.1 原始数据
var source = [
{
id: 1,
pid: 0,
name: '江苏省'
},{
id: 2,
pid: 1,
name: '南京市'
},{
id: 7,
pid: 0,
name: '上海市'
}, {
id: 3,
pid: 2,
name: '鼓楼区'
},{
id: 6,
pid: 5,
name: '武汉市'
},{
id: 4,
pid: 2,
name: '玄武区'
},{
id: 5,
pid: 0,
name: '湖北省'
}]
1.2 js代码
function toTree(data) {
let result = [];
if (!Array.isArray(data)) {
return result;
}
data.forEach(item => {
delete item.children;
});
let map = {};
data.forEach(item => {
map[item.id] = item; // id为键,原数据每一项为值的map对象
});
data.forEach(item => {
let parent = map[item.pid]; // item的pid若与map对象的键相同,则item为父级节点
let label = "";
item.label = item.name;
if (parent) {
(parent.children || (parent.children = [])).push(item);
parent.children.forEach(_item => {
_item.label = _item.name;
});
} else {
result.push(item);
}
});
console.log(result)
return result;
}
toTree(source);
1.3 转换效果
参考文章(侵删)
2. 树型结构数据转换为一维数组
2.1 原始数据
const treeObj = {
id: '0',
name: '中国',
children:[
{
id: '1',
name:'湖北省',
children:[
{
id: '1-1',
name:'武汉市',
children:[
{
id: '1-1-1',
name:'武昌区',
},
]
},
]
},
{
id: '2',
name:'江苏省',
children:[
{
id: '2-1',
name:'南京市',
children:[
{
id: '2-1-1',
name:'玄武区',
}
]
},
{
id: '2-2',
name:'镇江市',
children:[
{
id: '2-2-1',
name:'句容市',
children: [
{
id: '2-2-1-1',
name:'下蜀镇',
},
]
},
{
id: '2-2-2',
name:'京口区'
},
]
},
]
},
{
id: '3',
name:'浙江省',
}
]
};
2.2 js代码
// 将treeObj中的所有对象,放入一个数组中,要求某个对象在另一个对象的children时,其parent_id是对应的另一个对象的id
// 其原理实际上是数据结构中的广度优先遍历
function tree2Array(treeObj, rootid) {
const temp = []; // 设置临时数组,用来存放队列
const out = []; // 设置输出数组,用来存放要输出的一维数组
temp.push(treeObj);
// 首先把根元素存放入out中
let pid = rootid;
const obj = deepCopy(treeObj);
obj.pid = pid;
delete obj['children'];
out.push(obj)
// 对树对象进行广度优先的遍历
while(temp.length > 0) {
const first = temp.shift();
const children = first.children;
if(children && children.length > 0) {
pid = first.id;
const len = first.children.length;
for(let i=0;i<len;i++) {
temp.push(children[i]);
const obj = deepCopy(children[i]);
obj.pid = pid;
delete obj['children'];
out.push(obj)
}
}
}
return out
}
console.log(tree2Array(treeObj, 'root'))
// 深拷贝
function deepCopy(obj){
// 深度复制数组
if(Object.prototype.toString.call(obj) === "[object Array]"){
const object=[];
for(let i=0;i<obj.length;i++){
object.push(deepCopy(obj[i]))
}
return object
}
// 深度复制对象
if(Object.prototype.toString.call(obj) === "[object Object]"){
const object={};
for(let p in obj){
object[p]=obj[p]
}
return object
}
}
2.3 转换效果
参考文章(侵删)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/141483.html