Javascript - how to start forEach loop at some index

前端 未结 6 1595
予麋鹿
予麋鹿 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:06

    Unfortunately Array#forEach iterates over every element in the given array, but you could apply a simple condition to determine to which elements (with specified index) apply the given function.

    i > 3 ? someFn(item) : null;
    ^ if index more than 3 - call the function
    

    var arr = [1,2,3,4,5,6,7];
    
    function someFn(elem){
      console.log(elem);
    }
    
    arr.forEach(function(item, i) {
      return i > 3 ? someFn(item) : null;
    })

提交回复
热议问题