In nodeJs is there a way to loop through an array without using array size?

后端 未结 8 913
后悔当初
后悔当初 2021-02-01 11:56

Let\'s say I have

myArray = [\'item1\', \'item2\']

I tried

for (var item in myArray) {console.log(item)}

It

8条回答
  •  生来不讨喜
    2021-02-01 12:48

    Use Iterators...

    var myarray = ['hello', ' hello again'];
    processArray(myarray[Symbol.iterator](), () => {
        console.log('all done')
    })
    function processArray(iter, cb) {
        var curr = iter.next()
        if(curr.done)
            return cb()
        console.log(curr.value)
        processArray(iter, cb)
    }
    

    More in depth overview: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols

提交回复
热议问题