3
I have the following array
Array
(
[0] => Array
(
[id] => 232
[menu] => Energia
[logo] => fa-bolt
[url] => #
[submenu] => Array
(
[0] => Array
(
[id] => 209
[menu] => Cadastrar Medidor
[logo] => fa-circle-o
[url] => medidor
)
[1] => Array
(
[id] => 336
[menu] => Cadastrar Grandeza
[logo] => fa-circle-o
[url] => grandeza
)
[2] => Array
(
[id] => 233
[menu] => Configuração de Conta
[logo] => fa-wrench
[url] => #
[submenu] => Array
(
[0] => Array
(
[id] => 182
[menu] => Configuração de Tarifa
[logo] => fa-circle-o
[url] => ponta
)
[1] => Array
(
[id] => 190
[menu] => Lista de configuração de limite
[logo] => fa-circle-o
[url] => limite/limiteSub
)
[2] => Array
(
[id] => 197
[menu] => Feriados
[logo] => fa-circle-o
[url] => ano
)
)
)
[3] => Array
(
[id] => 180
[menu] => Entrada e saída
[logo] => fa-circle-o
[url] => rele/rele
)
[4] => Array
(
[id] => 120
[menu] => Monitoramento
[logo] => fa-circle-o
[url] => medicao/monitoramento
)
[5] => Array
(
[id] => 166
[menu] => Tela Status
[logo] => fa-circle-o
[url] => medidor/telaStatus
)
[6] => Array
(
[id] => 168
[menu] => Conta
[logo] => fa-money
[url] => conta/conta-config
)
)
)
[1] => Array
(
[id] => 234
[menu] => Temperatura
[logo] => fa-sun-o
[url] => #
[submenu] => Array
(
[0] => Array
(
[id] => 235
[menu] => Cadastrar Medidor
[logo] => fa-circle-o
[url] => mt
)
[1] => Array
(
[id] => 169
[menu] => Monitoramento
[logo] => fa-circle-o
[url] => medicao/graficos1
)
)
)
)
This array I fill in the following form using php
$arrayMenu = array();
foreach ($sql as $key => $value) {
$arrayMenu[] = array(
'id' => $value->menu_id,
'menu' => $value->menu_nome,
'logo' => $value->menu_logo,
'url' => $value->menu_url
);
if( $value->count > 0){
$query = self::queryFn( $value->menu_id, $id );
foreach ($query as $key1 => $value1) {
$arrayMenu[$key]['submenu'][] = array(
'id' => $value1->menu_id,
'menu' => $value1->menu_nome,
'logo' => $value1->menu_logo,
'url' => $value1->menu_url,
);
if( $value1->count > 0 ){
$query1 = self::queryFn( $value1->menu_id, $id );
foreach ($query1 as $key2 => $value2) {
$arrayMenu[$key]['submenu'][$key1]['submenu'][] = array(
'id' => $value2->menu_id,
'menu' => $value2->menu_nome,
'logo' => $value2->menu_logo,
'url' => $value2->menu_url
);
}
}
}
}
}
I would like to do the same thing using javascript
I’m trying like this:
const { connection1, errorHandler } = deps
var menus = queryFn({ parent, user, connection1, errorHandler })
var arrMenu = []
menus.then(r => {
//console.log('r',r.result)
var res = r.result
for (var i = 0; i < res.length; i++) {
parent = res[i].menu_id
var menu = {
id: res[i].menu_id,
menu: res[i].menu_nome,
logo: res[i].menu_logo,
url: res[i].menu_url
}
arrMenu.push(menu)
if (res[i].count > 0) {
var submenus = queryFn({ parent, user, connection1, errorHandler })
submenus.then(s => {
var res1 = s.result
for (var j = 0; j < res1.length; j++) {
var submenu = {
id: res1[j].menu_id,
menu: res1[j].menu_nome,
logo: res1[j].menu_logo,
url: res1[j].menu_url
}
menu[i] = { submenu }
//menu['submenu'].push( submenu )
arrMenu.push( menu )
}
})
} else {
arrMenu.push(menu)
}
}
console.log(arrMenu)
})
But I’m only getting the following result
[ { id: 204, menu: 'Energia', logo: 'fa-bolt', url: '#' },
{ id: 206, menu: 'Temperatura', logo: 'fa-sun-o', url: '#' } ]
How do I have one nested array or array inside the other?
Which version of the Node you are using?
– Sorack
Version of Node is 10.12.0
– adventistaam