0

I have a an array in js it's something like this

menu = [
        {
            name: 'Item1',
            //submenuName: 'submenu-1',
        },
        {
            name: 'Item2',
            submenuName: 'submenu-2',
            sub: [
                {
                    name: 'Item2_1',
                    //submenuName: '',
                },
                {
                    name: 'Item2_2',
                    //submenuName: '',
                },
                {
                    name: 'Item2_3',
                    //submenuName: '',
                }
            ]
        },
        {
            name: 'Item3',
            //submenuName: 'submenu-3',
        }
    ]

And i need to list them in ul tag, but every level has to be closed before the other.

<ul data-menu="main">
  <li data-submenu>Item1</li>
  <li data-submenu='submenu-2'>Item2</li>
  <li data-submenu>Item3</li>
</ul>
<ul data-menu="submenu-2">
  <li data-submenu>Item2_1</li>
  <li data-submenu>Item2_2</li>
  <li data-submenu>Item2_3</li>
</ul>

and so on. I've mange to print them but only the first level. Cannot print the sub level.

2
  • 2
    "I've mange to print them but only the first level" <- Add this code to question, and your failed attempt to print sub level. Commented Jan 19, 2017 at 7:08
  • I use JQ to print them $.each and check with dose the array has a sub array. They try to list him too, but don't know how to close the previews ul tag after the first level, not before Commented Jan 19, 2017 at 7:13

1 Answer 1

1

If the menus need to be listed one after another and not nested, then maintain an array of menus to print and fill that array with submenus while printing parent menus.

Since you mentioned, that you're using jQuery, here is an example using this library.

function generateMenu (menu, container) {
  var menus = [{name: 'main', entries: menu}];

  while (menus.length) {
    var current = menus.shift();

    var ul = $("<ul />").attr('data-menu', current.name);

    $.each(current.entries, function (index, menuItem) {
      var li = $('<li />')
                  .attr('data-submenu', menuItem.submenuName || '')
                  .text(menuItem.name);

      if ($.isArray(menuItem.sub)) {
        menus.push({name: menuItem.submenuName, entries: menuItem.sub});
      }

      li.appendTo(ul);
    });

    ul.appendTo(container);
  }
}

generateMenu(menu, $('body'));

JSFiddle example

Sign up to request clarification or add additional context in comments.

5 Comments

$ is jQuery. The author said in a comment that he uses jQuery.
Ah, I missed the comment. Added the tag to the question.
And I added info to my answer that it uses jQuery.
Hmm. It doesn't print them. In the log shows only [object Object]
@PeyoPeev which logs do you mean? My JSFiddle example works for me, I can see the menu in the page. Can you please prepare a JSFiddle example with your code or a test website and post the URL here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.