从嵌套的集合模型javascript创建JSON
Create JSON from nested set model javascript
我有这样的数据结构,显示嵌套树中每个节点的深度:
[
{
"name": "ELECTRONICS",
"depth": 0
},
{
"name": "TELEVISIONS",
"depth": 1
},
{
"name": "TUBE",
"depth": 2
},
{
"name": "PLASMA",
"depth": 2
},
{
"name": "GAME CONSOLES",
"depth": 1
},
{
"name": "MP3 PLAYERS",
"depth": 1
},
{
"name": "FLASH",
"depth": 2
}]
我想用JavaScript/node.js/Angular将预览数据转换为一个分层的JSON,如下所示:
[{
"name": "ELECTRONICS",
"children": [
{
"name": "TELEVISIONS",
"children": [
{
"name": "TUBE"
},
{
"name": "PLASMA"
}]
},
{
"name": "GAME CONSOLES"
},
{
"name": "MP3 PLAYERS",
"children": [
{
"name": "FLASH"
}]
}]
}]
您可以使用Array#forEach
和一个数组来引用深度。
var data = [{ "name": "ELECTRONICS", "depth": 0 }, { "name": "TELEVISIONS", "depth": 1 }, { "name": "TUBE", "depth": 2 }, { "name": "PLASMA", "depth": 2 }, { "name": "GAME CONSOLES", "depth": 1 }, { "name": "MP3 PLAYERS", "depth": 1 }, { "name": "FLASH", "depth": 2 }],
tree = [];
data.forEach(function (a, i, aa) {
var lastDepth = (aa[i - 1] || {}).depth, o;
if (a.depth !== 0 && a.depth > lastDepth) {
o = this[lastDepth][this[lastDepth].length - 1]
o.children = o.children || [];
this[a.depth] = o.children;
}
this[a.depth].push({ name: a.name });
}, [tree]);
console.log(tree);
这可能有助于
// Code goes here
angular.module("app",[])
.controller("ctrl", function($scope, $log){
$scope.src = [
{
"name": "ELECTRONICS",
"depth": 0
},
{
"name": "TELEVISIONS",
"depth": 1
},
{
"name": "TUBE",
"depth": 2
},
{
"name": "PLASMA",
"depth": 2
},
{
"name": "GAME CONSOLES",
"depth": 1
},
{
"name": "MP3 PLAYERS",
"depth": 1
},
{
"name": "FLASH",
"depth": 2
}];
$scope.tree = _.findWhere($scope.src, {"depth":0});
function makeTree(parentNode){
var d = parentNode.depth +1;
var children = _.where($scope.src, {"depth" : parentNode.depth+1});
if(children!=null){
parentNode.children = children;
parentNode.children.forEach(function(c){
makeTree(c);
});
}
}
makeTree($scope.tree);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script data-require="underscore.js@1.5.2" data-semver="1.5.2" src="//cdn.jsdelivr.net/underscorejs/1.5.2/underscore-min.js"></script>
<body ng-app="app">
<div ng-controller="ctrl">
First Node : {{firstNode|json}}
<h1>tree</h1>
<pre>{{tree|json}}</pre>
</div>
</body>
这是神奇的
var json = [
{
"name": "ELECTRONICS",
"depth": 0
},
{
"name": "TELEVISIONS",
"depth": 1
},
{
"name": "TUBE",
"depth": 2
},
{
"name": "PLASMA",
"depth": 2
},
{
"name": "GAME CONSOLES",
"depth": 1
},
{
"name": "MP3 PLAYERS",
"depth": 1
},
{
"name": "FLASH",
"depth": 2
}];
var newJSON = [];
function createTree(parentID, i, node, depth)
{
node.children = [];
delete node.depth;
while (i < json.length && json[i].depth > parentID)
{
if (depth === json[i].depth)
{
node.children.push(json[i]);
}
else
{
createTree(json[i-1].depth, i, json[i-1], depth + 1);
}
i++;
}
if (node.children.length === 0)
{
delete node.children;
}
return node;
}
var parent = {};
parent = createTree(-1, 0, parent, 0);
console.log(parent.children);
JSON.stringify(parent.children);
我假设,如果下一个数组元素的深度相等,那么它是一个同级元素,如果它是次要的,那么它就是一个子元素,并且如果它更大,那么我们不必向这个分支添加更多的元素。所以,我创建一个分支,如果深度相同,我会将其添加到我们的父级,如果深度较小,我会创建一个新分支,如果它较大,我会停止创建分支并将子级添加到"我的"父级。
编辑
我会选择Nina Scholz的解决方案,因为它更容易理解
相关文章:
- 从javascript创建一个列表
- 使用Javascript创建测验页面
- 获取对使用Tampermonkey使用javascript创建的元素的引用
- Jquerymobile-使用javascript创建选择菜单
- 使用SVG和JavaScript创建波浪动画
- 用javascript创建一个看起来正常分布的模式
- 如何在Ol3弹出窗口中添加用javascript创建的按钮上的点击事件
- 在PHP邮件中发送javascript创建的内容
- Knockout绑定是膨胀的html(表单元格),如何使用javascript创建绑定或从父元素绑定
- 重置使用javascript创建的新tr的val
- 如何使用javascript创建颜色数组
- javascript创建日期的月份错误
- 如何使用JavaScript创建时间计数器
- 使用Javascript创建内部样式表
- 用javascript创建另一个对象的实例
- 如何使用HTML、CSS和JavaScript创建自动图片库幻灯片
- 使用Javascript创建的Cookie在C#代码中不可访问..任何原因或其他选择
- 用javascript创建对象的dynamic数组
- Javascript创建函数,以便在其他函数之间共享变量
- 使用javascript创建幻灯片效果