展平嵌套对象

2023-12-14

我必须将 JSON 转换为下面的格式,但在将其转换回来时遇到问题。

这是当前的格式

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

这是我需要的格式:

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

基本上,我必须将它嵌套在我正在使用的脚本中,但服务器希望看到它被展平,在当前格式中,第三个对象维度以“children”开头。我需要取消嵌套子对象并保持对象按照我需要的格式运行。


第一个解决方案,假设您不希望根据 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);

是的,我知道,辅助函数会产生副作用,但我已将它们限制在访问者函数中以减少伤害,并且还有改进的空间(至少根据 id 对结果数组进行排序),但我将把它们留给你

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

展平嵌套对象 的相关文章

随机推荐