How to remove last 2 elements?

How to trim a ul list using js. Here's an example:
the <ul>
the <li>text ...</li>
the <li>text ...</li>
the <li>text ...</li>
the <li>text ...</li>
the <li>text ...</li>
the <li>text ...</li>
</ul>

Tried many solutions, but nothing comes out. Need to display 4 li element
July 8th 19 at 11:28
1 answer
July 8th 19 at 11:30
Solution
var i = 0;
 $('ul li').each(function(){
i++;
if(i>4)
$(this).remove();
console.log(i);
 });

in the condition prescribed how many points you need to keep and remove the rest

Find more questions by tags HTMLjQuery