How To Get Every Nth Element Of Array In JavaScript?

Get every Nth Element of Array in JavaScript

In this article, we will share two ways of using the ‘for’ loop to get every Nth element of array in JavaScript and explain why we don’t use JavaScript Array methods. Both of them iterate over all array elements and iterate only on the Nth element of the array. Read on it now.

Get every Nth element of array in JavaScript

Now, we will go through each solution to get every Nth element of the array with specific examples, and from there, choose the best solution for your problem.

Iterate over all array elements

Example:

// Get every nth element of array 'oldArr'
function getNth(oldArr, nth) {
   const newArr = [];

   // Push every nth element of array 'oldArr' to array 'newArr'
   for (let i = 0; i < oldArr.length; i++) {
      if ((i + 1) % nth === 0) {
         newArr.push(oldArr[i]);
      }
   }

   return newArr;
}

// Fibonacci, nth = 2
console.log(getNth([0, 1, 1, 2, 3, 5, 8, 13], 2));

// Number prime, nth = 4
console.log(getNth([2, 3, 5, 7, 11, 13, 17, 19], 4));

Output: 

[ 1, 2, 5, 13 ]
[ 7, 19 ]

First, we create an empty array named ‘newArr’. Then, use a for loop to iterate through the original array’s elements (oldArr).

At each loop, we check if the sorted element’s index is divisible by ‘nth’. If true, this element will be added to newArr using the push() method.

We can also use JavaScript array methods to get every Nth element of array. For example, the filter() and map() methods. However, all these methods work on iterating over all array elements. This will slow down the program if the array has many elements (for example, 1000 or 10000 elements).

To speed up the program, we will iterate only on the Nth element of the array.

Iterate only on the Nth element of the array

Example:

// Get every nth element of array 'oldArr'
function getNth(oldArr, nth) {
   const newArr = [];

   // Push every nth element of array 'oldArr' to array 'newArr'
   for (let i = (nth - 1); i < oldArr.length; i = i + nth) {
      newArr.push(oldArr[i]);
   }

   return newArr;
}

// Fibonacci, nth = 2
console.log(getNth([0, 1, 1, 2, 3, 5, 8, 13], 2));

// Number prime, nth = 4
console.log(getNth([2, 3, 5, 7, 11, 13, 17, 19], 4));

Output: 

[ 1, 2, 5, 13 ]
[ 7, 19 ]

Similar to the first solution, we create an empty array named ‘newArr’. However, instead of using a for loop to iterate through all the original array elements (oldArr), we loop through the Nth element of this array.

Finally, we will add the element to the newArr array at each loop using the push() method.

Summary

To get every Nth element of array in JavaScript, we recommend using the second method – iterate over on the Nth element of the array. This is better if you deal with an array containing many elements. I hope the information in this article will be helpful to you. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *