Source: https://s0ands0.github.io/100-days-of-code/r001/068-javascript-zip-iterators/
// The same function but fully looping all arrays:
const zip = (rows) => rows[0].map((_,c)=>rows.map(row=>row[c]));
function* iterator(iterable) {
for(const item of iterable) yield item;
}
function* zipIterables(...iterables) {
const iterators = iterables.map(x => iterator(x));
while (true) {
const stats = [];
const results = [];
for (const iterable of iterators) {
const result = iterable.next();
stats.push(result.done);
results.push(result.value);
}
if (stats.every((stat) => stat === true)) {
break;
}
yield results;
}
}
console.log(Array.from(zipIterables([1, 2, 3], ['A', 'B'], ['I', 'II', 'III', 'IIII'])));
// results in:
[1, 'A', 'I']
[2, 'B', 'II']
[3, , 'III']
[ , , 'IIII']
532900cookie-checkJavascript Zip Iterables