【问题标题】:Restructure JSON array重构 JSON 数组
【发布时间】:2015-10-07 16:39:08
【问题描述】:

我正在尝试以下列方式重构 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 节点外,所有内容都符合预期输出。它返回对象数组,而我需要带有键值对的对象。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 好奇使用当前结构有什么问题或问题?似乎对我更友好的代码
  • @charlietfl,我使用的 JavaScript 库需要上述格式作为输入。

标签: javascript json


【解决方案1】:

你不能使用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;
}

【讨论】:

    【解决方案2】:
    function restruct(arr) {
        var result = {};
    
        arr.forEach(function(obj) {         
            if (obj.children) {
                obj.children = restruct(obj.children);
            }
    
            result[obj.id] = obj;
        });
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 2018-03-05
      • 2020-02-27
      • 1970-01-01
      • 2017-06-20
      • 2018-12-03
      • 1970-01-01
      • 2012-09-13
      相关资源
      最近更新 更多