diff options
Diffstat (limited to 'js/utility/iterators.js')
-rw-r--r-- | js/utility/iterators.js | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/js/utility/iterators.js b/js/utility/iterators.js index e40a07110..b83e5de82 100644 --- a/js/utility/iterators.js +++ b/js/utility/iterators.js @@ -28,4 +28,23 @@ window.items = function (obj) { })(); } }; -}
\ No newline at end of file +}; + +/** + * Iterates over a list and returns the item and index + * like: (let [index, value] of list) + * Similar to Pythons enumerate() iterator function + */ +window.enumerate = function (list) { + 'use strict'; + + return { + [Symbol.iterator]: function () { + return (function*() { + for (let counter = 0; counter < list.length; counter += 1) { + yield [counter, list[counter]]; + } + })(); + } + }; +};
\ No newline at end of file |