Home  >  Q&A  >  body text

javascript - Return tree JSON structure by specified depth

There is a piece of JSON, as follows:
According to the tree hierarchy, it is level 3. I now want to return to the first two levels and discard the third level. How can I use javascript code to achieve this?
Original structure:
[{

            'id': 1,
            'title': 'node1',
            'nodes': [
                {
                    'id': 11,
                    'title': 'node1.1',
                    'nodes': **[
                        {
                            'id': 111,
                            'title': 'node1.1.1',
                            'nodes': []
                        }**
                    ]
                },
                {
                    'id': 12,
                    'title': 'node1.2',
                    'nodes': []
                }
            ]
        }, {
            'id': 2,
            'title': 'node2',
            'nodes': [
                {
                    'id': 21,
                    'title': 'node2.1',
                    'nodes': []
                },
                {
                    'id': 22,
                    'title': 'node2.2',
                    'nodes': []
                }
            ]
        }, {
            'id': 3,
            'title': 'node3',
            'nodes': [
                {
                    'id': 31,
                    'title': 'node3.1',
                    'nodes': []
                }
            ]
        }]

Processed structure:

    [{
            'id': 1,
            'title': 'node1',
            'nodes': [
                {
                    'id': 11,
                    'title': 'node1.1',
                    **'nodes': []**
                },
                {
                    'id': 12,
                    'title': 'node1.2',
                    'nodes': []
                }
            ]
        }, {
            'id': 2,
            'title': 'node2',
            'nodes': [
                {
                    'id': 21,
                    'title': 'node2.1',
                    'nodes': []
                },
                {
                    'id': 22,
                    'title': 'node2.2',
                    'nodes': []
                }
            ]
        }, {
            'id': 3,
            'title': 'node3',
            'nodes': [
                {
                    'id': 31,
                    'title': 'node3.1',
                    'nodes': []
                }
            ]
        }]    
        
黄舟黄舟2733 days ago463

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-05-19 10:25:17

    It is more convenient to use recursion for tree operations

    depth is the number of reserved layers. Note that it is called for the node of that layer. 'id': 1,

    function removeNode (root, depth) {
      if (typeof root !== 'object' || typeof depth !== 'number' || !Array.isArray(root.nodes)) { return root }
      if (depth < 1) { return {} }
    
      if (depth === 1) {
        root.nodes = []
      } else {
        root.nodes.forEach(node => {
          removeNode(node, depth - 1)
        })
      }
    
      return root
    }

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-19 10:25:17

    You can just loop through the two levels of json and reassemble a new json.

    reply
    0
  • Cancelreply