Need help building a tree array from a flat array

i have a mysql db with only one table that gives me following output.

[{country:"austria","state":"upper austria","city":"vienna","plz":"1000","name":"test 1"},{country:"austria","state":"upper austria","city":"vienna","plz":"1000","name":"test 2"},{country:"austria","state":"upper austria","city":"vienna","plz":"1010","name":"test 3"},{country:"austria","state":"lower austria","city":"krems","plz":"2222","name":"test 4"}]

and i need something like that

{
    "data": [
        {
            "country": "austria",
            "state": "upper austria",
            "city": "vienna",
            "plz": "1000",
            "persons": [
                {
                    "name": "test 1",
                    "name": "test 2"
                }   
            ]
        },
        {
            "country": "austria",
            "state": "upper austria",
            "city": "vienna",
            "plz": "1010",
            "persons": [
                {
                    "name": "test 3"
                }   
            ]
        },
        {
            "country": "austria",
            "state": "lower austria",
            "city": "krems",
            "plz": "2222",
            "persons": [
                {
                    "name": "test 4"
                }   
            ]
        }
]}

at least it should be displayed like that

austria
--> upper austria
----> vienna
------>1000
--------> test 1
--------> test 2
------>1010
--------> test 3
--> lower austria
----> krems
------>2222
--------> test 4

i tried a lot of solutions, but i get not the right results…
does anyone have an example for me, how i can realize that?

thanks a lot
cheers martin

show one of those you think were more promising! so we can see what you are doing

note though that you can’t have properties in one object with the same name - so you can’t have it like this:

sorry for my english! i have no code, that is doing what i want, and my js skills are bad.
i have tried a couple of function like that link

check also the thing I added to my last post

Check this:

thanks, but i does not understand how that flatmap() should help. maybe you can create an example?

Sorry for my late. Show me, how you want to be your array ?

no problem, the website has to run last Friday, so I decided to use a table view instead of the grid view.

thx a lot!

1 Like