Conversion data tree structure with one-dimensional array of data

When we do the project, we sometimes need data in a tree structure of the hierarchical data tree display, or the need to return data for dismantling the structure of data in a single layer array. Here we summarize these two methods.

Array to a tree structure:

Data = var [
{ "ID": 2, "name": "first level 1", "pid": 0},
{ "ID":. 3, "name": "second level 1", "pid" : 2},
{ "ID":. 5, "name": "third level. 1", "PID":. 4},
{ "ID": 100, "name": "third level 2", "pid" :. 3},
{ "ID":. 6, "name": "third level 2", "pid":. 3},
{ "ID": 601, "name": "third level 2", "pid" :. 6},
{ "ID": 602, "name": "third level 2", "pid":. 6},
{ "ID": 603, "name": "third level 2", "pid" :} 6
];
// data array to a tree structure (that is, the principle is the key by setting the value of id, pid then go through the same whether this key, as compared with the sub-level data this data)
function arrayToJson (treeArray) {
var r = [];
tmpMap = {} var;
for (var I = 0, L = treeArray.length; I <L; I ++) {
// the id to each of the data as the key value of obj, data is stored as a temporary value to a target value inside
tmpMap [treeArray [I] [ "ID"]] = treeArray [I];
}
the console.log ( 'tmpMap', tmpMap)
for (I = 0, L = treeArray.length; I <L; I ++) {
var Key = tmpMap [treeArray [I] [ "PID"]];
the console.log ( 'Key', Key)
// each cycle pid data, if the object has the temporary key value, it means that there is data corresponding to the key Children, need to go Push
// If this is a child data belong to which data
IF (key) {
// If the data is not Children
IF (! Key [ "Children"]) {
Key [ "Children"] = [];
Key [ "Children"] Push (treeArray [I]);.
// If this data has Children
} the else {
Key [ " Children "] Push (treeArray [I]);.
}
} the else {
// If this is not Key value, it means that the data belongs can not be found, it means no parent, directly on the outermost layer
r.push ( treeArray [I]);
}
}
return R & lt
}
tree structure data is transferred in the form of a monolayer array data:

jsonarr = var [{
ID:. 1,
name: 'Beijing',
ProSort:. 1,
the remark: 'municipalities',
PID: '',
isEdit: to false,
Children: [{
ID: 35,
name: 'Chaoyang'
PID:. 1,
the remark: '',
isEdit: to false,
Children: []
}, {
ID: 36,
name: 'Haidian District',
PID:. 1,
the remark: '',
isEdit: to false,
Children: []
}, {
ID: 37 [,
name: 'Fangshan',
PID:. 1,
the remark: '',
isEdit: to false,
Children: []
}, {
ID: 38 is,
name: 'Fengtai',
PID:. 1,
the remark: ' ',
isEdit: to false,
children: []
}]
}, {
ID: 2,
name: 'Tianjin',
ProSort: 2,
the remark: 'municipalities',
PID:' ',
isEdit: to false,
Children: [{
ID: 41 is,
name: "North Star area',
PID: 2,
the remark: '',
isEdit: to false,
Children: [{
ID: 113,
name: 'Tianjin Avenue',
ProSort: 2,
the remark: 'road',
PID: '',
isEdit: to false,
Children: [ ]}]
}, {
ID: 42 is,
name: 'Hebei District',
PID: 2,
the remark: '',
isEdit: to false,
Children: []
}, {
ID: 43 is,
name: 'Xiqing',
PID : 2,
the remark: '',
isEdit: false,
Children: []
}]
}, {
ID:. 3,
name: 'Hebei',
ProSort:. 5,
the remark: 'Department',
PID: '',
isEdit: to false,
Children: [{
ID: 45,
name: ' Hengshui ',
PID:. 3,
the remark:' ',
isEdit: to false,
Children: []
}, {
ID: 46 is,
name:' Zhangjiakou ',
PID:. 3,
the remark:' ',
isEdit: to false,
Children: []
}]
}]
@ tree data structure in the form of an array of single transfer
function jsonToArray (Nodes) {
var R & lt = [];
IF (Array.isArray (Nodes)) {
for (var I = 0, L = Nodes. length; I <L; I ++) {
r.push (Nodes [I]); // get a new array into each item of data
IF (Array.isArray (Nodes [I] [ "children"]) && Nodes [I] [ "children"]. length> 0)
// recursive call exists when children, the spliced data to the new array, and deletes the Children
R & lt r.concat = (jsonToArray (Nodes [I] [ "Children"]));
Delete Nodes [I] [ "Children"]
}
}
return R & lt;
}
in order not to destroy the original data, we need data operation performed deep copy:

// deep copy
function the deepCopy (obj) {
var Object;
// copy an array depth
IF (Object.prototype.toString.call (obj) == "[the Array Object]") {
Object = [];
for (var = I 0; I <obj.length; I ++) {
object.push (the deepCopy (obj [I]))
}
return Object
}
// copy target depth
if (Object.prototype.toString.call (obj) == " [object Object ] ") {
Object = {};
for (var in P obj) {
Object [P] = obj [P]
}
return Object
}
}
calls and validation of two methods:

CopyData the deepCopy = var (jsonarr)
var BB = jsonToArray (CopyData)
the console.log ( 'switch array JSON', BB)
var arrayToJson AA = (Data)
the console.log ( 'array to a tree', AA)
 
--- -------------
Disclaimer: this article is CSDN blogger "juvenile blue depths of the past 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/qq_39009348/article/details/87365547

Guess you like

Origin www.cnblogs.com/vicky-li/p/11909762.html