Get an iterator to nth element of an array in JavaScript
This post will discuss how to get an iterator to the nth element of an array in JavaScript.
There are several ways to get an iterator to the nth element of an array in JavaScript. An iterator is an object that allows us to traverse through a collection of values, such as an array, by providing a next() function that returns the next value and a done property that indicates whether the iteration is finished. Here are some of them:
1. Using Array.entries() function
The Array.entries() function returns a new array iterator object that contains the key-value pairs for each index in the array. We can use this function to get an iterator to the nth element of the array by calling the next() function n times and ignoring the returned values until we reach the desired element. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Create an array with some values let arr = ["Red", "Green", "Blue", "Yellow"]; // Get iterator to the 3rd element let n = 3; let iterator = arr.entries(); // skip the first n-1 elements for (let i = 0; i < n - 1; i++) { // Call next() and ignore the returned values iterator.next(); } // get an iterator to the nth element let nthElement = iterator.next(); console.log(nthElement.value); // Output: [2, "Blue"] |
In this example, arr.entries() returns an iterator object that can be used for further iteration. By calling next() on the iterator object twice within the loop, we skip the first two elements and reach the third element at index 2. However, it requires ES6 support or a polyfill for older browsers.
2. Using a generator function
Another option is to use a custom function that can yield values using the yield keyword. We can use this function to create an iterator that returns the elements of an array one by one. We can use this function to get an iterator to the nth element of the array by calling its next() function n times. Here’s an example of this approach:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function* iterateArray(arr) { for (let item of arr) { // yield each element of the array yield item; } } // Create an array with some values let arr = ["Red", "Green", "Blue", "Yellow"]; // Get iterator to the 3rd element let n = 3; // get a generator object let iterator = iterateArray(arr); // skip the first n-1 elements for (let i = 0; i < n - 1; i++) { // Call next() and ignore the returned values iterator.next(); } // get an iterator to the nth element let nthElement = iterator.next(); console.log(nthElement.value); // Output: "Blue" |
This function also returns an iterator object that can be used for further iteration. However, it also requires ES6 support or a transpiler for older browsers.
3. Using a custom function
We can also write our own function that takes an array and an index as arguments and returns an iterator to the nth element of the array. We can use a closure to store the array and the index in a local scope and return an object with a next() function that increments the index and returns the corresponding value from the array. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// Define a function that takes an array and an index as arguments, // and returns an iterator to the nth element of the array function getIterator(array, index) { // Check if the index is negative if (index < 0) { throw new Error("Invalid index"); } // Return an object with a next() function return { next: function() { // Check if the iteration is done if (index >= array.length) { return {value: undefined, done: true}; } // Return the value and increment the index return {value: [index, array[index++]], done: false}; } }; } // Create an array with some values let arr = ["Red", "Green", "Blue", "Yellow"]; // Get iterator to the 3rd element let n = 3; // Get an iterator to the nth element of the array let iter = getIterator(arr, n - 1); // Call next() and get the value let result = iter.next(); console.log(result.value); // Output: [2, "Blue"] |
That’s all about getting an iterator to the nth element of an array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)