Last active
September 11, 2019 13:57
-
-
Save xx7y7xx/2dc682f6996ac962bbfbfd7977ef6c1a to your computer and use it in GitHub Desktop.
Unflatten NC ref data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Unflatten NC ref data | |
* | |
* https://gist.github.com/xxd3vin/2dc682f6996ac962bbfbfd7977ef6c1a | |
* | |
* input: | |
* | |
* ```json | |
* [ | |
* { | |
* "refcode": "F2276", | |
* "pk_group": "0001H210000000000IGL", | |
* "id": "0001H2100000006Y1OQQ", | |
* "pid": "0001H21000000000239V", | |
* "pk_corp": "0001H2100000000022LJ", | |
* "enablestate": "2", | |
* "refname": "南陵陈英豪房地产开发有限公司", | |
* "refpk": "0001H2100000006Y1OQQ" | |
* }, | |
* { | |
* "refcode": "201", | |
* "pk_group": "0001H210000000000IGL", | |
* "id": "0001H21000000000239V", | |
* "pid": "0001H2100000000022YO", | |
* "pk_corp": "0001H2100000000022LJ", | |
* "enablestate": "2", | |
* "refname": "安徽区域", | |
* "refpk": "0001H21000000000239V" | |
* }, | |
* { | |
* "refcode": "2", | |
* "pk_group": "0001H210000000000IGL", | |
* "id": "0001H2100000000022YO", | |
* "pid": "0001H2100000000022LJ", | |
* "pk_corp": "0001H2100000000022LJ", | |
* "enablestate": "2", | |
* "refname": "房产公司", | |
* "refpk": "0001H2100000000022YO" | |
* }, | |
* { | |
* "refcode": "1", | |
* "pk_group": "0001H210000000000IGL", | |
* "id": "0001H2100000000022YK", | |
* "pid": "0001H2100000000022LJ", | |
* "pk_corp": "0001H2100000000022YK", | |
* "enablestate": "2", | |
* "refname": "集团总部", | |
* "refpk": "0001H2100000000022YK" | |
* }, | |
* { | |
* "refcode": "bgy", | |
* "pk_group": "0001H210000000000IGL", | |
* "id": "0001H2100000000022LJ", | |
* "pid": "null", | |
* "pk_corp": "0001H2100000000022LJ", | |
* "enablestate": "2", | |
* "refname": "陈英豪集团", | |
* "refpk": "0001H2100000000022LJ" | |
* } | |
* ] | |
* ``` | |
* | |
* Output: | |
* | |
* ```json | |
* { | |
* "id": "0001H2100000000022LJ", | |
* "name": "陈英豪集团", | |
* "children": { | |
* "0001H2100000000022YO": { | |
* "id": "0001H2100000000022YO", | |
* "name": "房产公司", | |
* "children": { | |
* "0001H21000000000239V": { | |
* "id": "0001H21000000000239V", | |
* "name": "安徽区域", | |
* "children": { | |
* "0001H2100000006Y1OQQ": { | |
* "id": "0001H2100000006Y1OQQ", | |
* "name": "南陵陈英豪房地产开发有限公司" | |
* } | |
* } | |
* } | |
* } | |
* }, | |
* "0001H2100000000022YK": { | |
* "id": "0001H2100000000022YK", | |
* "name": "集团总部" | |
* } | |
* } | |
* } | |
* ``` | |
*/ | |
function unflatten(data) { | |
var rootItem; | |
var createNodeForItem = function(item) { | |
// 是否创建过item对应的节点 | |
if (!item.node) { | |
item.node = { | |
id: item.id, | |
name: item.refname | |
}; | |
} | |
return item.node; | |
}; | |
var createNode = function(data) { | |
return { | |
id: data.id, | |
name: data.refname | |
} | |
}; | |
var getItem = function(arr, id) { | |
var filtedArr = arr.filter(function(item) { return item.id === id }); | |
return filtedArr.length ? filtedArr[0] : null; | |
}; | |
data.map(function (item) { | |
var node, parentNode, parentItem; | |
// ignore root node | |
if (item.pid === 'null') { | |
rootItem = item; | |
return; | |
} | |
parentItem = getItem(data, item.pid); | |
// 是否创建过item对应的节点 | |
if (!item.node) { | |
item.node = createNode(item); | |
} | |
node = item.node; | |
// 是否创建过parent item对应的节点 | |
if (!parentItem.node) { | |
parentItem.node = createNode(parentItem); | |
} | |
parentNode = parentItem.node; | |
// 将当前node添加到parent node上 | |
if (!parentNode.children) { | |
parentNode.children = {}; | |
} | |
parentNode.children[item.id] = node; | |
}); | |
return rootItem.node; | |
} | |
/************************************************************************ | |
Test case | |
var flattenData = [ | |
{ | |
"refcode": "F2276", | |
"pk_group": "0001H210000000000IGL", | |
"id": "0001H2100000006Y1OQQ", | |
"pid": "0001H21000000000239V", | |
"pk_corp": "0001H2100000000022LJ", | |
"enablestate": "2", | |
"refname": "南陵陈英豪房地产开发有限公司", | |
"refpk": "0001H2100000006Y1OQQ" | |
}, | |
{ | |
"refcode": "201", | |
"pk_group": "0001H210000000000IGL", | |
"id": "0001H21000000000239V", | |
"pid": "0001H2100000000022YO", | |
"pk_corp": "0001H2100000000022LJ", | |
"enablestate": "2", | |
"refname": "安徽区域", | |
"refpk": "0001H21000000000239V" | |
}, | |
{ | |
"refcode": "2", | |
"pk_group": "0001H210000000000IGL", | |
"id": "0001H2100000000022YO", | |
"pid": "0001H2100000000022LJ", | |
"pk_corp": "0001H2100000000022LJ", | |
"enablestate": "2", | |
"refname": "房产公司", | |
"refpk": "0001H2100000000022YO" | |
}, | |
{ | |
"refcode": "1", | |
"pk_group": "0001H210000000000IGL", | |
"id": "0001H2100000000022YK", | |
"pid": "0001H2100000000022LJ", | |
"pk_corp": "0001H2100000000022YK", | |
"enablestate": "2", | |
"refname": "集团总部", | |
"refpk": "0001H2100000000022YK" | |
}, | |
{ | |
"refcode": "bgy", | |
"pk_group": "0001H210000000000IGL", | |
"id": "0001H2100000000022LJ", | |
"pid": "null", | |
"pk_corp": "0001H2100000000022LJ", | |
"enablestate": "2", | |
"refname": "陈英豪集团", | |
"refpk": "0001H2100000000022LJ" | |
} | |
]; | |
var expect = { | |
"id": "0001H2100000000022LJ", | |
"name": "陈英豪集团", | |
"children": { | |
"0001H2100000000022YO": { | |
"id": "0001H2100000000022YO", | |
"name": "房产公司", | |
"children": { | |
"0001H21000000000239V": { | |
"id": "0001H21000000000239V", | |
"name": "安徽区域", | |
"children": { | |
"0001H2100000006Y1OQQ": { | |
"id": "0001H2100000006Y1OQQ", | |
"name": "南陵陈英豪房地产开发有限公司" | |
} | |
} | |
} | |
} | |
}, | |
"0001H2100000000022YK": { | |
"id": "0001H2100000000022YK", | |
"name": "集团总部" | |
} | |
} | |
}; | |
var unflattenData = unflatten(flattenData); | |
JSON.stringify(unflattenData) === JSON.stringify(expect); // true | |
************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment