4 ms·
I‘m not good in JavaScript, but reading the code, I wonder if this implementation doesn’t have an issue with truthiness in the second array. For example if you
by rmetzler 4y ago
I‘m not good in JavaScript, but reading the code, I wonder if this implementation doesn’t have an issue with truthiness in the second array. For example if you zip([true,true],[false,false]).
The minimum length approach seams to be the better one. (Without having it run anywhere.)
- universa1 4y agoThat was my quick reaction as well... I think it would need something like the python example below, where you first check the lengths of the two arrays/lists, and then iterate over that, and not directly over the length of the first list. iter_limit = Math.min(ls1.length, ls2.length); for(let i = 0; i < iter_limit; i++)...
- chrisco255 4y agofunction zip(arr_a, arr_b) { const len = arr_a.length < arr_b.length ? arr_a.length : arr_b.length; return arr_a.slice(0, len).map((el, idx) => [el, arr_b[idx]]); }
- m-r-r 4y agoYes, the JavaScript implementation doesn't works if the second array contains 'false', 'null', 'undefined', '0' or 'NaN'. Here is my implementation of the 'zip' function in modern JavaScript. It is similar to the Vlang version, but it is generic over the number of arrays : const zip = (...arrays) => { const length = Math.min(...arrays.map((array) => array.length)); return Array.from({ length }, (_, i) => arrays.map((array) => array[i])); };