Return Array inside the other

Asked

Viewed 203 times

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?

  • Version of Node is 10.12.0

1 answer

3


You have some problems in your execution:

  • There is no mention of the value in the first execution of the variable parent;
  • No mention of variable value user;
  • Submenu completion is asynchronous, and there is nothing to wait for completion before showing the result;
  • The code snippet menu[i] = { submenu } will replace the whole parent menu object and leave only the attribute submenu;
  • There is no treatment for N submenus levels.

I rewrote your entire code by applying async/await and performing the corrections due to the previously mentioned items (less those of variable values):

const { connection1, errorHandler } = deps;

const transformar = async ({ menu_id: id, menu_nome: nome, menu_logo: logo, menu_url: url, count: quantidade }) => {
  const menu = {
    id,
    logo,
    url,
    menu: nome,
  };

  if (quantidade > 0) menu.submenu = await preencher(id);

  return menu;
};

const preencher = async (pai) => {
  const { resultado } = await queryFn({ user, connection1, errorHandler, parent: pai });
  const promessas = resultado.map(transformar);
  const menus = await Promise.all(promessas);

  return menus;
};

To test the execution use:

(async () => {
  console.log(await preencher());
})();
  • I’m getting the following result Promise { <pending> }

  • @adventistaam tested as it is now

  • Yes! It worked. Return the same way

  • @adventistaam worked or returned the same way wrong? hahaha

  • 1

    rss. It worked! Return the way I returned in php

  • Thank you so much for your help!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.