Can anyone help me figure out how to create an iterator for my array in TypeScript without encountering a transpilation error? Is it possible using generics or some other method?
let array4 = [10, 20, 30];
array4[Symbol.iterator] = function () {
let i = 0;
return {
next: function() {
i++;
return {
value: i < 4 ? array4[i - 1] : undefined,
done: i >= 4 ? true : false
};
}
};
};
let it4 = array4[Symbol.iterator]();
console.log(it4.next());
console.log(it4.next());
console.log(it4.next());
console.log(it4.next());