How to convert these two data formats into each other through JS?
The same content, the flat ones are combined into nested ones, and the nested ones are disassembled into flat formats
I couldn’t figure it out after a long time, please help!
[
{
"id":1,
"number":"100.000",
"name": "admin.",
"level":1,
"children": [
{
"id": 2,
"number": "100.210",
"name": "admin.marketing",
"level": 2
},
{
"id": 3,
"number": "100.260",
"name": "admin.operation",
"level": 2,
"children": [
{
"id": 5,
"number": "260.261",
"name": "operation.content",
"level": 3
},
{
"id": 6,
"number": "260.262",
"name": "operation.promote",
"level": 2
},
{
"id": 7,
"number": "260.263",
"name": "operation.service",
"level": 2
}
]
},
{
"id": 4,
"number": "100.280",
"name": "admin.development",
"level": 2
}
]
}
]
[
{
"id":1,
"number":"100.000",
"name": "admin.",
"level":1
},
{
"id": 2,
"number": "100.210",
"name": "admin.marketing",
"level": 2
},
{
"id": 3,
"number": "100.260",
"name": "admin.operation",
"level": 2
},
{
"id": 4,
"number": "100.280",
"name": "admin.development",
"level": 2
},
{
"id": 5,
"number": "260.261",
"name": "operation.content",
"level": 3
},
{
"id": 6,
"number": "260.262",
"name": "operation.promote",
"level": 3
},
{
"id": 7,
"number": "260.263",
"name": "operation.service",
"level": 3
}
]
Using recursion to implement
The outermost data is an array. In fact, it can be considered as the outer children attribute, and the child elements of data may themselves have children.
Now the reverse method has been completed with the help of group friend owl!