I need help with a JavaScript code that will add elements from an array to each list on my webpage. The goal is to add numbers 1 through 4 to the list items, but only up to the number of items in the array. If there are only three list items, I want the script to stop at 3 and not add another item. Additionally, I want to remove any zeros present.
Any assistance would be greatly appreciated!
var levels = [ "1", "2", "3", "4" ];
$('ul li').each(function (x) {
$(this).append(levels[x]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul>
<li>0</li>
<li>0</li>
<li>0</li>
<li>0</li>
</ul>
<ul>
<li>0</li>
<li>0</li>
<li>0</li>
</ul>
<ul>
<li>0</li>
<li>0</li>
<li>0</li>
<li>0</li>
</ul>