I rarely work with JS, but I had to and unexpectedly discovered a very strange (in my opinion) behavior of standard cycles. Those. I write for example for (let index in data) , but in most languages, the element itself goes with the loop parameter, not its key ... I even stuck on this error for a short while before I realized that it was the key that was transmitted there. And how can I do something to return the item too? Or just write the let item = data[index] ? Well, what kind of tuple can come back if they are there ... Or something else.

For example, as in python: for key, value in data.iteritems():

    2 answers 2

    In the latest versions of the language entered for..of

     var arr = ['a', 'b', 'c'] for (let i of arr) { console.log(i); } 

    In addition, you can use the Object.entries and Object.values methods (these methods can be applied to any objects, not only to arrays)

     var arr = ['a', 'b', 'c'] console.log(JSON.stringify(Object.entries(arr))); console.log(JSON.stringify(Object.values(arr))); 

    For example, as in python: for key, value in data.iteritems():

    You can use destructuring when traversing:

     var arr = ['a', 'b', 'c'] for (let [key, value] of Object.entries(arr)) { console.log(key, value); } 

      Use for ... of ...

       for (let value of data) { console.log(value); } 

      https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Statements/for...of

      Pay attention to browser support!

      IE does not support this design.

      • Fine! And if I still need the key element? Can I get a loop variant with a key and an element? Or just use Kalbeki like array.forEach ()? - PECHAIR