twitch
Streaming on Twitch twice a week

Combining arrays pairwise

JavaScript

Need to combine values from two arrays pairwise? This function takes the two first values, the two second values, and so on and puts them in nested arrays. The operation is called “zip” because it works like a zipper.

// takes two arrays and combines their elements pairwise
const zip = (firstArray, secondArray) => {
  const longerLength = Math.max(firstArray.length, secondArray.length)
  const indices = [...new Array(longerLength).keys()]

  return indices.reduce((list, index) => [
    ...list,
    [firstArray[index], secondArray[index]]
  ], [])
}

zip([4, '8 oz', '2 cups', 'some'], ['eggs', 'milk', 'flour', 'salt'])
// ⇒ [[4, 'eggs'], ['8 oz', 'milk'], ['2 cups', 'flour'], ['some', 'salt']]

// if one array is shorter, that list’s value in a pair is `undefined`
zip([1, 2, 3], ['a', 'b'])       // ⇒ [[1, 'a'], [2, 'b'], [3, undefined]]
zip([1, 2],    ['a', 'b', 'c'])  // ⇒ [[1, 'a'], [2, 'b'], [undefined, 'c']]

More fire tips

Replacing all matches in a string

The String prototype’s replace function only replaces the first occurrence of a substring by default. We can extend that with a global flag on the expression.

JavaScriptregular expressions
Read fire tip

Read all fire tips →