DEV Community

Discussion on: 1 line of code: How to get every odd item of an Array

 
lexlohr profile image
Alex Lohr

If you are already using a for loop, you can also jump 2 steps on every iteration:

const oddItems = (arr, odds=[], i) => { for (i = 1; i < arr.length; i = i + 2) odds.push(arr[i]); return odds}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Haha... yeah - oops

Some comments have been hidden by the post's author - find out more