Javascript - how to start forEach loop at some index

前端 未结 6 1634
予麋鹿
予麋鹿 2021-01-04 00:31

I have an array with alot of items, and I am creating a list of them. I was thinking of paginating the list. I wonder how can I start a forEach or for

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 01:00

    Thinking on what @NinaScholz commented, perhaps you can use variables and any changes would be set in those instead of changing the loop.

    function someFn(item, array2){
      array2.push(item, array2);
    }
    
    var arrayItems1 = [1,2,3,4,5,6,7,8,9,10];
    var arrayItems2 = [];
    
    var firstIndex = 1;
    var lastIndex = 5;
    var i = 0;
    
    for (i = firstIndex; i < lastIndex; i++){
         someFn(arrayItems1[i], arrayItems2);
    }
    
    alert(arrayItems2.join(' '));

提交回复
热议问题