console
var data = {
"nodeList": [
{
"id": "1",
"nodeName": "a"
},
{
"id": "1.1",
"nodeName": "b"
},
{
"id": "1.2",
"nodeName": "c"
},
{
"id": "1.3",
"nodeName": "d"
},
{
"id": "1.1.1",
"nodeName": "e"
},
{
"id": "1.1.2",
"nodeName": "f"
},
{
"id": "1.2.1",
"nodeName": "g"
},
{
"id": "1.2.2",
"nodeName": "h"
},
{
"id": "1.1.1.1",
"nodeName": "i"
},
{
"id": "1.1.1.2",
"nodeName": "j"
},
{
"id": "1.2.1.1",
"nodeName": "k"
},
{
"id": "1.2.1.2",
"nodeName": "l"
}
],
"lineList": [
{
"from": '1',
"to": '1.1'
},
{
"from": '1',
"to": '1.2'
},
{
"from": '1',
"to": '1.3'
},
{
"from": '1.1',
"to": '1.1.1'
},
{
"from": '1.1',
"to": '1.1.2'
},
{
"from": '1.2',
"to": '1.2.1'
},
{
"from": '1.2',
"to": '1.2.2'
},
{
"from": '1.1.1',
"to": '1.1.1.1'
},
{
"from": '1.1.1',
"to": '1.1.1.2'
},
{
"from": '1.2.1',
"to": '1.2.1.1'
},
{
"from": '1.2.1',
"to": '1.2.1.2'
}
]
}
var operationData = this.dataFormatting(data)
console.log('结果输出 = ' + JSON.stringify(operationData))
function dataFormatting(data) {
var _flowData = this
var operationParas = {}
operationParas = _flowData.getRootDirection(data.lineList, data.nodeList)
return operationParas
}
function getNodeInfo(nodeId, nodeList) {
var nodeInfo = {}
for (var i = 0; i < nodeList.length; i++) {
if (nodeId === nodeList[i].id) {
nodeInfo = nodeList[i]
}
}
return nodeInfo
}
function getRootDirection(lineList, nodeList) {
console.log('获取根目录节点 => Begin')
var _rootDir = this
var rootOperation = {}
var rootNodeId = lineList[0].from
var rootNodeInfo = _rootDir.getNodeInfo(rootNodeId, nodeList)
console.log('获取根目录节点信息 => ' + JSON.stringify(rootNodeInfo))
var hasChildren = _rootDir.hasChildren(rootNodeId, lineList)
console.log('获取根目录节点信息,判断是否有子节点 => ' + JSON.stringify(hasChildren))
if (hasChildren) {
rootNodeInfo.children = []
console.log('获取根目录节点信息,进一步获取子节点 => 「rootNodeId」= ' + rootNodeId)
rootNodeInfo = _rootDir.getChildrenDirection(rootNodeInfo, rootNodeId, lineList, nodeList)
}
rootOperation = rootNodeInfo
return rootOperation
}
function getChildrenDirection(nodeInfo, parentId, lineList, nodeList) {
var _childrenDir = this
for (var i = 0; i < lineList.length; i++) {
if (lineList[i].from === parentId) {
var childrenId = lineList[i].to
console.log("获取子节点信息,子节点 Id = " + childrenId)
var childrenItem = _childrenDir.getNodeInfo(childrenId, nodeList)
var hasChildren = _childrenDir.hasChildren(childrenId, lineList)
if (hasChildren) {
childrenItem.children = []
rootNodeInfo = _childrenDir.getChildrenDirection(childrenItem, childrenId, lineList, nodeList)
} else {
nodeInfo.children.push(childrenItem)
}
}
}
return nodeInfo
}
function hasChildren(nodeId, lineList) {
var hasChildren = false
if (nodeId) {
for (var i = 0; i < lineList.length; i++) {
if (nodeId === lineList[i].from) {
hasChildren = true
}
}
}
return hasChildren
}
1<br>
1.1<br>
1.1.1<br>
1.1.1.1<br>
1.1.1.2<br>
1.1.2<br>
1.2<br>
1.2.1<br>
1.2.1.1<br>
1.2.1.2<br>
1.2.2<br>
1.3<br>