Javascript – how to start forEach loop at some index

You could use a copy of the array, by using Array#slice

The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

array.slice(10, 20).forEach(someFn); // only for functions which respects API of forEach*

* parameters for a callback

Or you can start at a given index and end at a given index.

for (var i = 10, len = Math.min(20, arr.length); i < len; i++) {
    someFn(arr[i]);
}

With

Math.min(20, arr.length)

returns a value, if the array is smaller than the given value 20. For example if the array has only index 0 … 14, you get as result 15.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)