js 将接收到的数组转换成需要的格式

有如下这种格式

{
    "2": {
        "id": 2,
        "name": "demo1",
        "lv1": {
            "4": {
                "id": 20,
                "name": "demo1-1",
                "lv2": {
                    "2": {
                        "id": 2,
                        "name": "demo1-1-1"
                    },
                    "3": {
                        "id": 3,
                        "name": "demo1-1-2"
                    }
                }
            },
            "7": {
                "id": 23,
                "name": "demo1-2"
            }
        }

    },

    "3": {
        "id": 4,
        "name": "demo2",
        "lv1": {
            "6": {
                "id": 34,
                "name": "demo2-1",
                "lv2": {
                    "3": {
                        "id": 3,
                        "name": "demo2-1-1"
                    }
                }
            },
            "8": {
                "id": 55,
                "name": "demo2-2"
            }
        }
    }
}

需要转换成这种格式

[{
        "value": "2",
        "text": "demo1",
        "children": [{
            "value": "20",
            "text": "demo1-1",
            "children": [{
                    "value": "2",
                    "text": "demo1-1-1"
                },
                {
                    "value": "3",
                    "text": "demo1-1-2"
                }
            ]
        }]
    },
    {
        "value": "4",
        "text": "demo2",
        "children": [{
                "value": "34",
                "text": "demo2-1",
                "children": [{
                    "value": "3",
                    "text": "demo2-1-1"
                }]
            },
            {
                "value": "55",
                "text": "demo2-2"
            }
        ]
    }
]

要求新数据value跟原数据id是对应关系,新手调试了一整天都没效果,求大牛解答。

阅读 1.9k
2 个回答

比较乱, 可以参考下:

var data = {
    "2": {
        "id": 2,
        "name": "demo1",
        "lv1": {
            "4": {
                "id": 20,
                "name": "demo1-1",
                "lv2": {
                    "2": {
                        "id": 2,
                        "name": "demo1-1-1"
                    },
                    "3": {
                        "id": 3,
                        "name": "demo1-1-2"
                    }
                }
            },
            "7": {
                "id": 23,
                "name": "demo1-2"
            }
        }

    },

    "3": {
        "id": 4,
        "name": "demo2",
        "lv1": {
            "6": {
                "id": 34,
                "name": "demo2-1",
                "lv2": {
                    "3": {
                        "id": 3,
                        "name": "demo2-1-1"
                    }
                }
            },
            "8": {
                "id": 55,
                "name": "demo2-2"
            }
        }
    }
};
function fn (obj) {
    var arr = [];
    for(var key in obj) {
        var child = null;
        for(var subKey in obj[key]) {
            if(/lv/.test(subKey)) {
                child = fn(obj[key][subKey]);
            }
        }
        if(child != null) {
            arr.push(
                {
                    "value": key,
                    "text": obj[key].name,
                    "children": child
                }
            );
        } else {
            arr.push(
                {
                    "value": key,
                    "text": obj[key].name
                }
            );
        }
    }
    return arr;
}

fn(data);

对树遍历,重新生成数组,前端做这工作,挺耗性能的,尤其数据量庞大的时候,可能会卡死,一般我们都让后端处理好再给我们

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题