Create JSON from a nested javascript set model

I have this data structure that shows the depth of each node in a nested tree:

[
  {
    "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
  }]

I would like to convert the preview data using JavaScript / node.js / Angular to hierarchical JSON as follows:

[{
    "name": "ELECTRONICS",
    "children": [
      {
        "name": "TELEVISIONS",
        "children": [
          {
            "name": "TUBE"
          },
          {
            "name": "PLASMA"
          }]
     }, 
     {
        "name": "GAME CONSOLES"
     }, 
     {
        "name": "MP3 PLAYERS",
        "children": [
          {
            "name": "FLASH"
         }]
    }]
}]
+4
source share
3 answers

You can use an Array#forEacharray to reference depth.

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);
Run codeHide result
+4
source

Here is the magic

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);
Run codeHide result

, , , , , . , , , , , , , "" .

,

0

This can help

// 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>
Run codeHide result
0
source

Source: https://habr.com/ru/post/1648696/


All Articles