Thursday, 8 June 2017

Reversing Multi-level list

How can i reverse a list taking as reference the "child" order or "hierarchical" order, something like this list:

[
    {
        "id": 6,
        "name": "Item6",
        "parent": {
            "id": 2,
            "name": "Item2",
            "parent": {
                "id": 1,
                "name": "Item1"
            }
        }
    },
    {
        "id": 5,
        "name": "Item5",
        "parent": {
            "id": 4,
            "name": "Item4",
            "parent": {
                "id": 3,
                "name": "Item3",
                "parent": {
                    "id": 1,
                    "name": "Item1"
                }
            }
        }
    }
]

so output list should be like this:

[
    {
        "id": 1,
        "name": "Item1",
        "parent": {
            "id": 2,
            "name": "Item2",
            "parent": {
                "id": 6,
                "name": "Item6"
            }
        }
    },
    {
        "id": 1,
        "name": "Item1",
        "parent": {
            "id": 3,
            "name": "Item3",
            "parent": {
                "id": 4,
                "name": "Item4",
                "parent": {
                    "id": 5,
                    "name": "Item5"
                }
            }
        }
    }
]

is there a native function of NodeJS (JavaScript) that resolve this case?

something like listItems.reverseLevels();



via Ninja Coding

No comments:

Post a Comment