Skip to content

Instantly share code, notes, and snippets.

@18601673727
Created September 18, 2014 08:12
Show Gist options
  • Save 18601673727/773db55577eae221cbc0 to your computer and use it in GitHub Desktop.
Save 18601673727/773db55577eae221cbc0 to your computer and use it in GitHub Desktop.
Json to Tree conversion.
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