This function returns every n-th item from an array, starting from the first item.
JavaScript:
let nthItems = (arr, n) => arr.filter((_, i) => i % n === 0);
// Example
console.log(nthItems(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 2)); // ['a', 'c', 'e', 'g']
console.log(nthItems([10, 20, 30, 40, 50, 60, 70, 80], 3)); // [10, 40, 70]
TypeScript:
let nthItems = (arr: T[], n: number): T[] => arr.filter((_, i) => i % n === 0);
// Example
console.log(nthItems(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 2)); // ['a', 'c', 'e', 'g']
console.log(nthItems([10, 20, 30, 40, 50, 60, 70, 80], 3)); // [10, 40, 70]