1

I'm trying to restructure the JSON array in following manner. In the output, I need id as key and object itself as it's value.

Sample input:

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

Required output:

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

The following code gives me almost the required answer.

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

The output is:

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

If you notice, everything is as per expected output except the children nodes. It returns array of objects while I need object with key-value pairs. Can anybody help me with this?

2
  • 3
    Curious what is wrong or problematic with using current structure? Seems more code friendly to me Commented Oct 7, 2015 at 16:42
  • @charlietfl, A JavaScript library I'm using requires above format as input. Commented Oct 7, 2015 at 16:44

2 Answers 2

3

You can't use map because it returns an array, you can use forEach instead, like:

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

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

        result[obj.id] = obj;
    });
    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

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

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.