Skip to content

Instantly share code, notes, and snippets.

@andytwoods
Created April 25, 2017 19:09
Show Gist options
  • Save andytwoods/7fc343a8f74b82f9db6e21b044c9a525 to your computer and use it in GitHub Desktop.
Save andytwoods/7fc343a8f74b82f9db6e21b044c9a525 to your computer and use it in GitHub Desktop.
interweave two arrays. Can be different sizes.
export default function interleave(arr1, arr2){
"use strict";
let combined = []
let arr1_len = arr1.length
let arr2_len = arr2.length
while(arr1_len>0 || arr2_len >0){
if(arr1_len>0){
combined.push(arr1.shift())
arr1_len--
}
if(arr2_len>0){
combined.push(arr2.shift())
arr2_len--
}
}
return combined
}
console.log(interleave([1,1,1,1],[2])) // [1, 2, 1, 1, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment