Created
September 12, 2019 08:50
-
-
Save jknair0/fccc32c6928080588b2ccaa58f1e1dc5 to your computer and use it in GitHub Desktop.
zip two js arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static zip(array1, array2) { | |
| if (!array1 || !array2) return []; | |
| const l1 = array1.length; | |
| const l2 = array2.length; | |
| const minLen = l1 < l2 ? l1 : l2; | |
| const minIndex = minLen > 0 ? minLen - 1 : minLen; | |
| const array1sliced = array1.slice(0, minIndex); | |
| const array2sliced = array2.slice(0, minIndex); | |
| return array1sliced.map((item1, index) => [item1, array2sliced[index]]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment