Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从嵌套的集合模型javascript创建JSON_Javascript_Angularjs_Json_Node.js - Fatal编程技术网

从嵌套的集合模型javascript创建JSON

从嵌套的集合模型javascript创建JSON,javascript,angularjs,json,node.js,Javascript,Angularjs,Json,Node.js,我的数据结构显示嵌套树中每个节点的深度: [ { "name": "ELECTRONICS", "depth": 0 }, { "name": "TELEVISIONS", "depth": 1 }, { "name": "TUBE", "depth": 2 }, { "name": "PLASMA", "depth": 2 }, { "name": "GAME CONSOLES",

我的数据结构显示嵌套树中每个节点的深度:

[
  {
    "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"
         }]
    }]
}]
可以使用和数组作为深度的引用

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播放器”,“depth”:1},{“name”:“FLASH”,“depth”:2}],
树=[];
data.forEach(函数(a,i,aa){
var lastDepth=(aa[i-1]|{});
如果(a.depth!==0&&a.depth>lastDepth){
o=此[lastDepth][此[lastDepth]。长度-1]
o、 children=o.children | |[];
这[a.深度]=o.儿童;
}
此[a.depth].push({name:a.name});
},[树];
控制台日志(树)这就是魔法

var json=[
{
“名称”:“电子产品”,
“深度”:0
},
{
“名称”:“电视”,
“深度”:1
},
{
“名称”:“管”,
“深度”:2
},
{
“名称”:“血浆”,
“深度”:2
},
{
“名称”:“游戏机”,
“深度”:1
},
{
“名称”:“MP3播放器”,
“深度”:1
},
{
“名称”:“闪光”,
“深度”:2
}];
var newJSON=[];
函数createTree(parentID、i、节点、深度)
{
node.children=[];
删除节点深度;
而(iparentID)
{
if(depth==json[i].depth)
{
node.children.push(json[i]);
}
其他的
{
createTree(json[i-1]。深度,i,json[i-1],深度+1);
}
i++;
}
if(node.children.length==0)
{
删除node.children;
}
返回节点;
}
var parent={};
parent=createTree(-1,0,parent,0);
console.log(parent.children);
JSON.stringify(parent.children)这可能会有所帮助

//代码在这里
角度模块(“应用程序”,[])
.controller(“ctrl”,函数($scope,$log){
$scope.src=[
{
“名称”:“电子产品”,
“深度”:0
},
{
“名称”:“电视”,
“深度”:1
},
{
“名称”:“管”,
“深度”:2
},
{
“名称”:“血浆”,
“深度”:2
},
{
“名称”:“游戏机”,
“深度”:1
},
{
“名称”:“MP3播放器”,
“深度”:1
},
{
“名称”:“闪光”,
“深度”:2
}];
$scope.tree=\.findWhere($scope.src,{“depth”:0});
函数makeTree(parentNode){
var d=parentNode.depth+1;
var children=\.where($scope.src,{“depth”:parentNode.depth+1});
如果(子项!=null){
parentNode.children=子节点;
parentNode.children.forEach(函数(c){
makeTree(c);
});
}
}
makeTree($scope.tree);
});

第一个节点:{firstNode | json}
树
{{tree}json}

遗憾的是,深度未知