javascript - 重构 JSON 数组

标签 javascript json

我正在尝试按照以下方式重组 JSON 数组。在输出中,我需要 id 作为键和对象本身作为它的值。

示例输入:

[
    {
        "id": "1",
        "children": [
            {
                "id": "1-1",
                "children": [
                    {
                        "id": "1-1-1",
                        "children": []
                    },
                    {
                        "id": "1-1-2",
                        "children": []
                    }
                ]
            },
            {
                "id": "1-2",
                "children": []
            }
        ]
    },
    {
        "id": "2",
        "children": []
    },
    {
        "id": "3",
        "children": [
            {
                "id": "3-1",
                "children": []
            }
        ]
    }
]

要求的输出:

{
    "1": {
        "id": "1",
        "children": {
            "1-1": {
                "id": "1-1",
                "children": {
                    "1-1-1": {
                        "id": "1-1-1",
                        "children": []
                    },
                    "1-1-2": {
                        "id": "1-1-2",
                        "children": []
                    }
                }
            },
            "1-2": {
                "id": "1-2",
                "children": []
            }
        }
    },
    "2": {
        "id": "2",
        "children": []
    },
    "3": {
        "id": "3",
        "children": {
            "3-1": {
                "id": "3-1",
                "children": []
            }
        }
    }
}

下面的代码几乎给出了所需的答案。

function restruct(arr) {
    var newArray = arr.map(function(obj) {
        var t = {};
        if (obj.children)
            obj.children = restruct(obj.children);
        t[obj.id] = obj;
        return t;
    });
    return newArray;
}

输出是:

[
    {
        "1": {
            "id": "1",
            "children": [
                {
                    "1-1": {
                        "id": "1-1",
                        "children": [
                            {
                                "1-1-1": {
                                    "id": "1-1-1",
                                    "children": []
                                }
                            },
                            {
                                "1-1-2": {
                                    "id": "1-1-2",
                                    "children": []
                                }
                            }
                        ]
                    }
                },
                {
                    "1-2": {
                        "id": "1-2",
                        "children": []
                    }
                }
            ]
        }
    },
    {
        "2": {
            "id": "2",
            "children": []
        }
    },
    {
        "3": {
            "id": "3",
            "children": [
                {
                    "3-1": {
                        "id": "3-1",
                        "children": []
                    }
                }
            ]
        }
    }
]

如果您注意到,除 children 节点外,一切都符合预期输出。它返回对象数组,而我需要带有键值对的对象。有人可以帮我解决这个问题吗?

最佳答案

你不能使用 map 因为它返回一个数组,你可以使用 forEach 代替,比如:

function restruct(arr) {
    var result = {};

    arr.forEach(function(obj) {         
        if (obj.children) {
            obj.children = restruct(obj.children);
        }

        result[obj.id] = obj;
    });
    return result;
}

关于javascript - 重构 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32997827/

相关文章:

PHP 解析来自 SOAP 客户端的 SOAP XML 响应

javascript - 使用 Javascript 将 JSON 转换为 CSV 不会给出 key

json - 创建用户帐户时正确的 HATEOAS 响应

javascript - 如何将我的 Vue js SPA 部署到我的 Django 服务器中?

javascript - 为什么 CSS 选择器 'table tr:not(tr:nth-child(even))' 会抛出 TypeError?

java - Servlet 无法识别 JSON?

node.js - ExpressJS 请求正文中 JSON 中的反斜杠 - 使用 body-parser

javascript - 如何从最左边的点开始获取 Highcharts X 轴类别

javascript - 使用 Jquery 确定语言切换

javascript - 如何从另一个 JS 数组创建新的 JS 数组