A
A
Alex Ozerov2020-05-02 18:58:10
JavaScript
Alex Ozerov, 2020-05-02 18:58:10

What is the point of an object iterator?

Hello, I'm learning js, I've reached the topic of object iterators. I understand how the code works:

let nam = {
    from: 1,
    to: 5
}

nam[Symbol.iterator] = function(){
    return {
        current: this.from,
        last: this.to,

        next(){
            if(this.current <= this.last){
                return {
                    done:false,
                    value: this.current++
                } 
            }else{
                return {done: true}
            }
        }
    }
}


for(let x of nam){console.log(x)}

but that's what iterators of objects are for, I can't figure it out. learnjavascript explains dryly and with only one example. Perhaps you (experienced comrades) have examples where the iterator is used, then I will be glad to study your code or explanation. Thanks in advance for your attention.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question