array to tree by js
来源:互联网 发布:阿里云监控系统 编辑:程序博客网 时间:2024/06/08 15:18
function formatDataToTree(arr) {
var tree = [],
mappedArr = {},
arrElem,
mappedElem;
// First map the nodes of the array to an object -> create a hash table.
for(var i = 0, len = arr.length; i < len; i++) {
arrElem = arr[i];
mappedArr[arrElem._id] = arrElem;
mappedArr[arrElem._id]['childrens'] = [];
}
for (var id in mappedArr) {
if (mappedArr.hasOwnProperty(id)) {
mappedElem = mappedArr[id];
// If the element is not at the root level, add it to its parent array of children.
if (mappedElem.parentTagId) {
mappedArr[mappedElem['parentTagId']]['childrens'].push(mappedElem);
}
// If the element is at the root level, add it to first level elements array.
else {
tree.push(mappedElem);
}
}
}
return tree;
}
var tree = [],
mappedArr = {},
arrElem,
mappedElem;
// First map the nodes of the array to an object -> create a hash table.
for(var i = 0, len = arr.length; i < len; i++) {
arrElem = arr[i];
mappedArr[arrElem._id] = arrElem;
mappedArr[arrElem._id]['childrens'] = [];
}
for (var id in mappedArr) {
if (mappedArr.hasOwnProperty(id)) {
mappedElem = mappedArr[id];
// If the element is not at the root level, add it to its parent array of children.
if (mappedElem.parentTagId) {
mappedArr[mappedElem['parentTagId']]['childrens'].push(mappedElem);
}
// If the element is at the root level, add it to first level elements array.
else {
tree.push(mappedElem);
}
}
}
return tree;
}
阅读全文
0 0
- array to tree by js
- js array to json
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- opencv 写视频时找不到编码器问题解决方法
- 关于ShareSDK微信的一个坑
- SpannableString相关工具类
- Spring源码解析之一 ------ 默认标签的解析注册(IOC的第一步)
- 端口号及分类协议号
- array to tree by js
- Spring依赖注入最易懂解释
- 常用的WebService网站
- @ResponseBody
- openssl源码安装
- HDU 1695 数论 容斥 欧拉函数 || 莫比乌斯反演
- 调通sina33下的AP6212A0版本的BT(V1.0)
- 2017大预测:大数据、物联网与人工智能
- 美丽诗文