O array JavaScript abaixo é o resultado de uma consulta MySQL:
json-database.json
[
{
"id": 1,
"node_name": "SP2 - Virtual Private Serve Product",
"children_name": "Virtual Private Server CPU",
"node_id": 1
},
{
"id": 1,
"node_name": "SP2 - Virtual Private Serve Product",
"children_name": "Virtual Private Server Hard Disk",
"node_id": 1
},
{
"id": 1,
"node_name": "SP2 - Virtual Private Serve Product",
"children_name": "Virtual Private Server RAM",
"node_id": 1
},
{
"id": 2,
"node_name": "SP2 - Virtual Private Serve Product",
"children_name": "Virtual Private Server CPU",
"node_id": 2
},
{
"id": 2,
"node_name": "SP2 - Virtual Private Serve Product",
"children_name": "Virtual Private Server Hard Disk",
"node_id": 2
},
{
"id": 2,
"node_name": "SP2 - Virtual Private Serve Product",
"children_name": "Virtual Private Server RAM",
"node_id": 2
}
]
Consulta MySQL:
SELECT N.id, N.name AS node_name, C.id AS children_id, C.name AS children_name, C.node_id
FROM tab_nodes AS N
INNER JOIN tab_nodes_children AS C
ON N.id = C.node_id
Preciso converter o array em um objeto JavaScript com a estrutura abaixo:
nodes = [
{
id: 1,
name: 'SP2 - Virtual Private Serve Product',
children: [
{
id: 1,
name: 'Virtual Private Server CPU'
},
{
id: 2,
name: 'Virtual Private Server Hard Disk'
},
{
id: 3,
name: 'Virtual Private Server CPU'
},
]
},
{
id: 2,
name: 'SP2 - Virtual Private Serve Product',
children: [
{
id: 4,
name: 'Virtual Private Server CPU'
},
{
id: 5,
name: 'Virtual Private Server Hard Disk'
},
{
id: 6,
name: 'Virtual Private Server CPU'
},
]
},
]
Estou tentando utilizar o reduce com Object.assign, mas está dando erro.