Darian Darian - 1 year ago 90
HTML Question

Continue counting when Index parameter is set to a value

I have made an each loop which counts a number of posts, using an index parameter.

I want to set that index param to a certain value and then it counts from there.

But when I do set it to a value in the loop, it doesn't continue counting, it just outputs that assigned value for the number of objects in the loop.

I have attached snippets below and a screenshot of the console.log result.


$.each(articleArray[0], function(i) {
i = postID;
articleContent = articleArray[(pageNumber - 1)][i].content;
$('.swiper-slide:nth-child(' + slideCount + ')').html(articleContent);


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Console Screen

Answer Source

Don't use the index parameter. Use your own variable you create outside the $.each loop:

var counter = postID;
$.each(articleArray[0], function() {
    articleContent = articleArray[(pageNumber - 1)][counter++].content;
    $('.swiper-slide:nth-child(' + slideCount + ')').html(articleContent);

Note that I moved where the increment on it was, from the console.log (which I assume is temporary) to the actual use of it. If you meant it to start out at postID + 1 (which is what incrementing it in the console.log did), just add the + 1 to the first line defining counter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download