Hey experts, I'm on the hunt for the most efficient method to transform this data:
[
{ "id": 1, "animal": "cat", "age": 6, "name": "loky" },
{ "id": 2, "animal": "cat", "age": 3, "name": "michu", "parent": 1 },
{ "id": 3, "animal": "cat", "age": 2, "name": "boots", "parent": 1 },
{ "id": 4, "animal": "dog", "age": 9, "name": "bones" },
{ "id": 5, "animal": "dog", "age": 6, "name": "chok", "parent": 4 },
{ "id": 6, "animal": "dog", "age": 6, "name"": "cofee","parent": 4 }
]
into the following format:
[
{ "id": 1,
"animal": "cat",
"age": 6,
"name": "loky",
"childs":[ { "id": 2, "animal": "cat", "age": 3, "name": "michu", "parent": 1 },
{ "id": 3, "animal": "cat", "age": 2, "name": "boots", "parent": 1 }]
}
,
{ "id": 4,
"animal": "dog",
"age": 9,
"name": "bones",
"childs":[{ "id": 5, "animal": "dog", "age": 6, "name": "chok", "parent": 4 },
{ "id": 6, "animal": "dog", "age": 6, "name": "cofee", "parent": 4 }]
}
]
Remember, utilize the "parent" key to nest under "childs". I managed to find a solution but it involved multiple functions, surely there's a more efficient way.
Thanks in advance and please pardon any language mistakes.