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:
myIterableis 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 thedataproperty of themyIterableobject and returns it as thevalueproperty of the iterator object. - When the iteration is complete, the
doneproperty of the iterator object is set totrue.
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:
myStringis a string, and you can use afor...ofloop to iterate over its characters directly.- Each character in the string will be assigned to the
charvariable 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:
myArrayis an array, and you can use afor...ofloop to iterate over its elements directly.- Each element in the array will be assigned to the
elementvariable in the loop, and you can perform operations on it as needed.