展平嵌套对象 [英] Flattening a nested object

查看:41
本文介绍了展平嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将 JSON 转换为以下格式,我在将其转换回来时遇到问题.

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

这是当前格式

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

基本上,我必须为我正在使用的脚本嵌套它,但服务器希望看到它变平,在当前格式中,第三个对象维度以子级"开头.我需要取消嵌套子项并使对象按照我需要的格式运行.

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.

推荐答案

第一个解决方案,假设您不希望基于 id 对结果数组进行排序:

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);

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

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

这篇关于展平嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆