Skip to main content

Javascript Object Iterables

JavaScript iterables are objects that implement the iterable protocol, which allows them to be iterated over using a for...of loop or the Symbol.iterator method.

Iterables are used to represent collections of values or sequences of data, and they provide a consistent way to iterate over elements in a collection.

Here's an example of how you can create and use an iterable object:

// Define an iterable object
const myIterable = {
data: [1, 2, 3, 4, 5],
[Symbol.iterator]() {
let index = 0;
return {
next: () => {
if (index < this.data.length) {
return {value: this.data[index++], done: false};
} else {
return {value: undefined, done: true};
}
},
};
},
};

// Iterate over the values in the iterable object using a for...of loop
for (const value of myIterable) {
console.log(value); // Output: 1 2 3 4 5
}

In the above example:

  • myIterable is an iterable object that represents a collection of values.
  • It has a Symbol.iterator method that returns an iterator object with a next() method.
  • The next() method retrieves the next value in the iteration from the data property of the myIterable object and returns it as the value property of the iterator object.
  • When the iteration is complete, the done property of the iterator object is set to true.

Iterating Over a String​

A string is an iterable object, which means you can iterate over its characters using a for...of loop or by manually implementing the iterable protocol with the Symbol.iterator method.

Here's an example:

// Example string
const myString = "Hello";

// Iterate over the characters in the string using a for...of loop
for (const char of myString) {
console.log(char); // Output: "H" "e" "l" "l" "o"
}

// Manually implement the iterable protocol to iterate over the characters in the string
const stringIterator = myString[Symbol.iterator]();
let next = stringIterator.next();
while (!next.done) {
console.log(next.value); // Output: "H" "e" "l" "l" "o"
next = stringIterator.next();
}

In the above example:

  • myString is a string, and you can use a for...of loop to iterate over its characters directly.
  • Each character in the string will be assigned to the char variable in the loop, and you can perform operations on it as needed.

Iterating Over an Array​

Arrays are iterable objects, which means you can iterate over their elements using a for...of loop or by manually implementing the iterable protocol with the Symbol.iterator method.

As an example:

// Example array
const myArray = [1, 2, 3, 4, 5];

// Iterate over the elements in the array using a for...of loop
for (const element of myArray) {
console.log(element); // Output: 1 2 3 4 5
}

// Manually implement the iterable protocol to iterate over the elements in the array
const arrayIterator = myArray[Symbol.iterator]();
let next = arrayIterator.next();
while (!next.done) {
console.log(next.value); // Output: 1 2 3 4 5
next = arrayIterator.next();
}

In the above example:

  • myArray is an array, and you can use a for...of loop to iterate over its elements directly.
  • Each element in the array will be assigned to the element variable in the loop, and you can perform operations on it as needed.