js array return only certain positions

Solutions on MaxInterview for js array return only certain positions by the best coders in the world

showing results for - "js array return only certain positions"
Jacob
23 Jun 2018
1const every_nth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
2console.log(every_nth([1, 2, 3, 4, 5, 6], 1));
3console.log(every_nth([1, 2, 3, 4, 5, 6], 2));
4console.log(every_nth([1, 2, 3, 4, 5, 6], 3));
5console.log(every_nth([1, 2, 3, 4, 5, 6], 4));
6
7// OUTPUT
8
9[1,2,3,4,5,6]
10[2,4,6]
11[3,6]
12[4]