I have the following class:
class Group {
constructor() {
this.group = [];
}
add(input) {
if (!this.group.includes(input)) {
this.group.push(input);
}
}
static from(arr) {
const group = new Group();
for (let el of arr) {
group.add(el);
}
this.group = group;
return group;
}
has(elem) {
return this.group.includes(elem);
}
delete(elem) {
let newarr = this.group.filter((n) => n !== elem);
this.group = newarr;
}
}
what I want to do is this:
for (let value of Group.from(["a", "b", "c"])) {
console.log(value);
}
// → a
// → b
// → c
that is to make the class an iterable object but I dont know how to do it.
note:
don’t just return the iterator created by calling the Symbol.iterator method on the array