Created
September 18, 2014 08:12
-
-
Save 18601673727/773db55577eae221cbc0 to your computer and use it in GitHub Desktop.
Json to Tree conversion.
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
var tree = []; | |
var transData = function (a, idStr, pidStr, childrenStr){ | |
var r = [], hash = {}, id = idStr, pid = pidStr, children = childrenStr, i = 0, j = 0, len = a.length; | |
for(; i < len; i++){ | |
hash[a[i][id]] = a[i]; | |
} | |
for(; j < len; j++){ | |
var aVal = a[j], hashVP = hash[aVal[pid]]; | |
if (hashVP) { | |
!hashVP[children] && (hashVP[children] = []); | |
hashVP[children].push(aVal); | |
} else { | |
r.push(aVal); | |
} | |
} | |
return r; | |
} | |
tree = transData(res.data, 'id', 'parentid', 'children'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment