How can I skip the first iteration in a forEach loop? I've got my forEach loop working as expected but I need to start on the second item totally ignoring the first item. I'm using ES6.
cars.forEach(car => {...do something});
I thought I could maybe do something like
cars.skip(1).forEach(car => {...do something});
you need to check index, and use return on that value, what you need. In your case you need to skip zero index (0), here is code
const cars = ['audi', 'bmw', 'maybach']
cars.forEach((car, index) => {
if (index === 0) return;
console.log(car)
});
this code will show 'bmw' and 'maybach'
Writing out my comment as an answer:
cars.slice(1).forEach(car => { do something });
MDN reference to slice method: https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With