0
I have a foreach
which prints all categories of a JSON
. How do I make him take only up to 3 position and stop the loop
? The point is I want to create a menu ul
with 3 categories and within it create another li
with a sub-menu listing the rest of them. If there are 10 categories then it lists the top 3 in the top 3 li
and then at 7 in this submenu inside the li
next.
Follows the code:
var quantidadeElementos = retorno.data.length;
var i = 0;
while (i <= quantidadeElementos)
{
if (i == 3)
{
retorno.data.forEach(function(item)
{
console.log(item.nome);
})
break;
}
i++;
}
loop in loop?
– adventistaam
Wouldn’t it be better to just make a cup like this?
var quantidadeElementos = retorno.data.length.slice(0, 3);
?– Sergio
or so
$.each(retorno.data, function(i, j){ if( i < 3 ) console.log(j.nome); else break; })
– adventistaam
Thank you worked perfectly. My mistake was not passing the parameters necessary for the function to work
– Felipe Michael da Fonseca