for-of-loop

How does `for..of` loop resolve the iterator from an object?

ぃ、小莉子 提交于 2020-01-11 09:29:39
问题 For an object to implement iterable interface it must implement [Symbol.iterator] key that points to a function that returns the iterator . I'm wondering if the for..of loop internally calls this method on an object to get that iterator ? The reason I'm curious is that, for example, Map defines an interface with several iterators (entries, values, keys) and it seems that if not specified explicitly the for..of loop uses the iterator returned by map.entries() call. I've trying searching in the

How can a Javascript object become iterable with for…of statement? [duplicate]

蓝咒 提交于 2019-12-31 21:33:12
问题 This question already has answers here : Using Objects in For Of Loops (14 answers) Closed 3 years ago . I would like to set the options[Symbol.iterator] property in order to iterate on the simple objects I create with the for...of statement : options = { male: 'John', female: 'Gina', rel: 'Love' }; for(let p of options){ console.log(`Property ${p}`); }; But this code gives me the following error: array.html:72 Uncaught TypeError: options[Symbol.iterator] is not a function How I can set the

for..in or for..of Object keys

ぃ、小莉子 提交于 2019-12-19 09:17:13
问题 So my IDE doesn't like when I use a for..in loop to iterate over an object keys. I get a warning: Possible iteration over unexpected (custom / inherited) members, probably missing hasOwnProperty check So I get what it's saying, so in that case would it be better to use something like for (const key of Object.keys(obj)) instead of for (const key in obj) ? Are there any real differences between the two, performance-wise? 回答1: There is a slight difference between looping though the Object.keys

Javascript for…of doesn't work in Safari

巧了我就是萌 提交于 2019-12-05 17:37:09
Currently I am trying to build a simple sidenavigation that appears/disappears whenever one of the "toggleSidenav" buttons is clicked (there are multiple). It seemed to work fine when testing with Firefox and Chrome but today when I tried to open my page with Safari (desktop and mobile version) the buttons didn't do anything. The problem seems to be the for-of-loop I used but checking the reference of the for...of , Safari should support it. My code: for (var btn of document.getElementsByClassName("toggleSidenav")) { btn.addEventListener("click", function() { var style = window

How can a Javascript object become iterable with for…of statement? [duplicate]

删除回忆录丶 提交于 2019-12-03 03:19:32
This question already has an answer here: Using Objects in For Of Loops 14 answers I would like to set the options[Symbol.iterator] property in order to iterate on the simple objects I create with the for...of statement : options = { male: 'John', female: 'Gina', rel: 'Love' }; for(let p of options){ console.log(`Property ${p}`); }; But this code gives me the following error: array.html:72 Uncaught TypeError: options[Symbol.iterator] is not a function How I can set the right iterator function on a simple object as above? Solved // define the Iterator for the options object options[Symbol

How does `for..of` loop resolve the iterator from an object?

喜欢而已 提交于 2019-12-01 19:45:34
For an object to implement iterable interface it must implement [Symbol.iterator] key that points to a function that returns the iterator . I'm wondering if the for..of loop internally calls this method on an object to get that iterator ? The reason I'm curious is that, for example, Map defines an interface with several iterators (entries, values, keys) and it seems that if not specified explicitly the for..of loop uses the iterator returned by map.entries() call. I've trying searching in the specification but it only specifies that iterator is passed as a parameter to the abstract operation

How to iterate over a Set or Map in reverse order in javascript?

大城市里の小女人 提交于 2019-12-01 05:51:58
I'm looking for a a way to iterate over a Set or Map in reverse order. Consider this simple example in regular order: var mySet = new Set([1,2,3,4,5]); for(let myNum of mySet) { console.log(myNum); // output: 1, 2, 3, 4, 5 in sepearte lines } The iterator given from Set.prototype.values() or Set.prototype.entries() are also from start to beginning. What would be the solution to iterate the Set (or Map) in the reverse order? Tamas Hegedus There is no way to obtain a reversed iterator on Maps or Sets, as I have found out while trying to get the last item added to a Set . So the only way really

How to iterate over a Set or Map in reverse order in javascript?

眉间皱痕 提交于 2019-12-01 04:05:19
问题 I'm looking for a a way to iterate over a Set or Map in reverse order. Consider this simple example in regular order: var mySet = new Set([1,2,3,4,5]); for(let myNum of mySet) { console.log(myNum); // output: 1, 2, 3, 4, 5 in sepearte lines } The iterator given from Set.prototype.values() or Set.prototype.entries() are also from start to beginning. What would be the solution to iterate the Set (or Map) in the reverse order? 回答1: There is no way to obtain a reversed iterator on Maps or Sets,

Access to ES6 array element index inside for-of loop

a 夏天 提交于 2019-11-26 21:23:07
We can access array elements using a for-of loop: for (const j of [1, 2, 3, 4, 5]) { console.log(j); } How can I modify this code to access the current index too? I want to achieve this using for-of syntax, neither forEach nor for-in. Michał Perłakowski Use Array.prototype.keys : for (const index of [1, 2, 3, 4, 5].keys()) { console.log(index); } If you want to access both the key and the value, you can use Array.prototype.entries() with destructuring : for (const [index, value] of [1, 2, 3, 4, 5].entries()) { console.log(index, value); } Array#entries returns the index and the value, if you

Access to ES6 array element index inside for-of loop

房东的猫 提交于 2019-11-26 06:16:07
问题 We can access array elements using a for-of loop: for (const j of [1, 2, 3, 4, 5]) { console.log(j); } How can I modify this code to access the current index too? I want to achieve this using for-of syntax, neither forEach nor for-in. 回答1: Use Array.prototype.keys: for (const index of [1, 2, 3, 4, 5].keys()) { console.log(index); } If you want to access both the key and the value, you can use Array.prototype.entries() with destructuring: for (const [index, value] of [1, 2, 3, 4, 5].entries())