{"id":5329,"date":"2021-09-21T11:08:58","date_gmt":"2021-09-21T10:08:58","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=5329"},"modified":"2022-07-25T08:35:41","modified_gmt":"2022-07-25T07:35:41","slug":"javascript-zip-iterables","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-zip-iterables\/","title":{"rendered":"Javascript Zip Iterables"},"content":{"rendered":"\n<p>Source: <a href=\"https:\/\/s0ands0.github.io\/100-days-of-code\/r001\/068-javascript-zip-iterators\/\">https:\/\/s0ands0.github.io\/100-days-of-code\/r001\/068-javascript-zip-iterators\/<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ The same function but fully looping all arrays:\nconst zip = (rows) => rows[0].map((_,c)=>rows.map(row=>row[c]));\n\nfunction* iterator(iterable) {\n\tfor(const item of iterable) yield item;\n}\n\nfunction* zipIterables(...iterables) {\n\tconst iterators = iterables.map(x => iterator(x));\n\t\n\twhile (true) {\n\t\tconst stats = [];\n\t\tconst results = [];\n\n\t\tfor (const iterable of iterators) {\n\t\t\tconst result = iterable.next();\n\t\t\tstats.push(result.done);\n\t\t\tresults.push(result.value);\n\t\t}\n\n\t\tif (stats.every((stat) => stat === true)) {\n\t\t\tbreak;\n\t\t}\n\t\tyield results;\n\t}\n}\n\nconsole.log(Array.from(zipIterables([1, 2, 3], ['A', 'B'], ['I', 'II', 'III', 'IIII'])));\n\n\/\/ results in:\n[1, 'A', 'I']\n[2, 'B', 'II']\n[3,    , 'III']\n[ ,    , 'IIII']\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/s0ands0.github.io\/100-days-of-code\/r001\/068-javascript-zip-iterators\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4,1],"tags":[],"class_list":["post-5329","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=5329"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5329\/revisions"}],"predecessor-version":[{"id":5355,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5329\/revisions\/5355"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=5329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=5329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=5329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}