Flattening a nested object

帅比萌擦擦* 提交于 2021-02-17 02:00:50

问题


I have to convert JSON to the format below, I'm having a problem converting it back.

Here is the current format

[{
    "id": "5",
    "parentid": "0",
    "text": "Device Guides",
    "index": 0,
    "children": [{
        "id": "10",
        "text": "Grandstream GXP-21XX",
        "index": 0
    }, {
        "id": "11",
        "text": "Polycom Soundstation/Soundpoint",
        "index": 1
    }, {
        "id": "23",
        "text": "New Polycom",
        "index": 2
    }]
}, {
    "id": "6",
    "parentid": "0",
    "text": "Pre-Sales Evaluation",
    "index": 1,
    "children": []
}, {
    "id": "7",
    "parentid": "0",
    "text": "Router Setup Guides",
    "index": 2,
    "children": [{
        "id": "9",
        "text": "Sonicwall",
        "index": 0
    }, {
        "id": "12",
        "text": "Cisco",
        "index": 1
    }]
}, {
    "id": "9",
    "parentid": "7",
    "text": "Sonicwall",
    "index": 3,
    "children": []
}, {
    "id": "10",
    "parentid": "5",
    "text": "Grandstream GXP-21XX",
    "index": 4,
    "children": []
}, {
    "id": "11",
    "parentid": "5",
    "text": "Polycom Soundstation/Soundpoint",
    "index": 5,
    "children": []
}, {
    "id": "12",
    "parentid": "7",
    "text": "Cisco",
    "index": 6,
    "children": []
}]

Here is the format I need it in:

[{
    "id": "5",
    "parentid": "0",
    "text": "Device Guides",
    "index": "0"
}, {
    "id": "6",
    "parentid": "0",
    "text": "Pre-Sales Evaluation",
    "index": "0"
}, {
    "id": "7",
    "parentid": "0",
    "text": "Router Setup Guides",
    "index": "0"
}, {
    "id": "9",
    "parentid": "7",
    "text": "Sonicwall",
    "index": "0"
}, {
    "id": "10",
    "parentid": "5",
    "text": "Grandstream GXP-21XX",
    "index": "0"
}, {
    "id": "11",
    "parentid": "5",
    "text": "Polycom Soundstation\/Soundpoint",
    "index": "0"
}, {
    "id": "12",
    "parentid": "7",
    "text": "Cisco",
    "index": "0"
}]

Basically, I have to nest it for the script I'm using but the server is expecting to see it flattened, in the current format the 3rd object dimension starts with "children". I need to unnest children and keep the objects going like the format I need it in.


回答1:


A first solution, granted you don't want the resulting array to be sorted based on the id:

function visitor(graph) {
  var i, l,
  nodes=[],
  visited=[];

  function clone(n) {
     // improve the function yourself I'm lazy
     var i,l,
         props=["id","parentid","index","text"],
         result={};
     for (i = 0, l = props.length; i < l; i++) { 
        if (n[props[i]]) {
          result[props[i]]= n[props[i]];
        }
     }
     return result;
  }

  function helper (node) {
    var i, limit;
    if (visited.indexOf(node.id) == -1) {
      visited.push(node.id);
      nodes.push(clone(node));
      if( node.children) {
        for (i = 0, limit = node.children.length; i < limit; i++) {
          helper(node.children[i]);
        }
      }
    }
  }

  for (i = 0, l = graph.length; i < l; i++) {
    helper(graph[i]);
  }

  return nodes;
}

var graph =     [{
    "id": "5",
    "parentid": "0",
    "text": "Device Guides",
    "index": 0,
    "children": [{
        "id": "10",
        "text": "Grandstream GXP-21XX",
        "index": 0
    }, {
        "id": "11",
        "text": "Polycom Soundstation/Soundpoint",
        "index": 1
    }, {
        "id": "23",
        "text": "New Polycom",
        "index": 2
    }]
}, {
    "id": "6",
    "parentid": "0",
    "text": "Pre-Sales Evaluation",
    "index": 1,
    "children": []
}, {
    "id": "7",
    "parentid": "0",
    "text": "Router Setup Guides",
    "index": 2,
    "children": [{
        "id": "9",
        "text": "Sonicwall",
        "index": 0
    }, {
        "id": "12",
        "text": "Cisco",
        "index": 1
    }]
}, {
    "id": "9",
    "parentid": "7",
    "text": "Sonicwall",
    "index": 3,
    "children": []
}, {
    "id": "10",
    "parentid": "5",
    "text": "Grandstream GXP-21XX",
    "index": 4,
    "children": []
}, {
    "id": "11",
    "parentid": "5",
    "text": "Polycom Soundstation/Soundpoint",
    "index": 5,
    "children": []
}, {
    "id": "12",
    "parentid": "7",
    "text": "Cisco",
    "index": 6,
    "children": []
}];

nodes = visitor(graph);

And yes, I know, the helper function relay on side effects but I've scoped them into the visitor function to reduce harm and there is room for improvements (at least sorting the resulting array based on the id) but I will leave them to you



来源:https://stackoverflow.com/questions/15984257/flattening-a-nested-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!