How to get every Nth element of array in Node.js

1 Answer

0 votes
function getEveryNthElement(arr, nth) {
    const result = [];

    for (let i = nth; i < arr.length; i += nth) {
    	    result.push(arr[i]);
    }

    return result;
}

const arr =  [4, 5, 6, 7, 1, 9, 0, 3, 31, 99, 18, 2, 60, 70];
const N = 3;

console.log(getEveryNthElement(arr, N));


  
  
  
  
/*
run:
  
[ 7, 0, 99, 60 ]
  
*/

 



answered May 18, 2022 by avibootz
...