Header logo.
Small Hallucinations
HomeArchiveTagsAboutFeed

Notes tagged with “code_idioms”

Emulating Python's zip() and zip_longest() in JavaScript

This is essentially how you emulate the way Python's zip() function works using JavaScript.

What makes this possible is the fact that JavaScript array methods — map(), forEach() and even filter() — take up to three arguments (element, index, array) while they iterate through the array.

In which element is the element that the iterator points to at each step, index is the index of this item in the original array. And it's worth noting the third argument is the original array.

Since map() method of the JavaScript array is more flexible than zip(), we can do more stuff to the iterated item or the array. But if you want to do anything with array, remember the original array changes accordingly. In this case, a: ⋯