adding function that make a new array containing every nth element of a given array.

This commit is contained in:
iamsoorena
2017-12-15 21:24:55 +03:30
parent 0c89d5ae69
commit a99d61cda5
2 changed files with 11 additions and 0 deletions

View File

@ -0,0 +1,10 @@
### Take every nth element of an array
Use `Array.filter()` to create a new array that contains every nth element of a given array.
```js
const everynth = (arr, nth) => arr.filter((e, i) => i % nth === 0)
/*
console.log(everynth([1,2,3,4,5,6], 2)) // -> [ 1, 3, 5 ]
*/
```