Files
30-seconds-of-code/snippets/take-every-nth-element.md
2017-12-16 13:56:55 +02:00

245 B

Take every nth element

Use Array.filter() to create a new array that contains every nth element of a given array.

const everynth = (arr, nth) => arr.filter((e, i) => i % nth === 0);
// everynth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ]