We need to make a three-level menu for the online store, which will open on click.
- How to make a multidimensional array, in which there will be only the names of these items of the 3-level menu? And in html, let's say with the help of the ejs template engine to take data from this multidimensional array, and form a menu. What is of interest is the creation of this array, in which will be the names of the menu, located on the level of nesting.
- How to create this menu correctly so that later it would be possible to create a multidimensional menu from this array in a loop?
- Please give a simple example of an array with a multi-level menu.
I understand here you need to use an object in which the key will be as a separate menu? Something like this needs to be done.
<% let catalog = [ { id: 1, title: 'Наборы для творчества', parent: 0 }, { id: 2, title: 'Научные игры', parent: 0 }, { id: 3, title: 'Настольные игры', parent: 0 }, { id: 4, title: 'Демонстрационные материалы', parent: 0 }, { id: 5, title: 'Раздаточные материалы', parent: 0 }, { id: 6, title: 'Рабочие тетради', parent: 0 }, { id: 7, title: 'Плакаты и таблицы', parent: 0 }, { id: 8, title: 'Оформление интерьера детского сада', parent: 0 }, { id: 9, title: 'Портфели, портфолио, детские анкеты', parent: 0 }, { id: 10, title: 'Для детской комнаты', parent: 0 }, { id: 11, title: 'Канцтовары', parent: 0 }, { id: 12, title: 'Новогодние товары', parent: 0 }, { id: 13, title: 'Большие наборы для творчества', parent: 1 }, { id: 14, title: 'Наборы для творчества из гипса', parent: 1 }, { id: 15, title: 'Картинки из песка, блесток, пайеток', parent: 1 }, { id: 16, title: 'HANDMADE - декор своими руками', parent: 1 }, { id: 17, title: 'Наборы для творчества на любой вкус', parent: 1 }, { id: 18, title: 'Наборы для слепков', parent: 1 }, { id: 19, title: 'Гравюры', parent: 1 }, { id: 20, title: 'Роспись по холсту', parent: 1 }, { id: 21, title: 'Раскраски и наклейки', parent: 1 }, { id: 22, title: 'Раскраски', parent: 13 }, { id: 23, title: 'Альбомы с наклейками', parent: 13 }, { id: 24, title: 'Наборы наклеек', parent: 13 } ] for(let i = 0; i < catalog.length; i++){ %> <% if(catalog[i]['parent'] == 0) { %> <li><%- catalog[i]['title'] %></li> <% } else {%> <ul> <li><%- catalog[i]['title'] %></li> </ul> <% }} %> 