Skip to content

Instantly share code, notes, and snippets.

@gerep
Created January 27, 2022 13:46
Show Gist options
  • Save gerep/11b97a0c942b77c5c0c160b45501e875 to your computer and use it in GitHub Desktop.
Save gerep/11b97a0c942b77c5c0c160b45501e875 to your computer and use it in GitHub Desktop.
function zipmap(keys, values){
if(keys.length === 0 || values.length === 0) {
return {}
}
obj = {[keys[0]]: values[0]}
return { ...obj, ...zipmap(keys.slice(1), values.slice(1))}
}
let zm = zipmap([ 1,2,3 ], [ 4,5,6 ])
console.log(zm[1])
console.log(zm[2])
console.log(zm[3])
zm = zipmap([ 1,2,3,45,6 ], [ 4,5,6,7,8,9 ])
console.log(zm[1])
console.log(zm[2])
console.log(zm[3])
console.log(zm[45])
console.log(zm[6])
zm = zipmap([ 'lane', 'elon', 'karen' ], [ 'wagner', 'musk', 'gilain' ])
console.log(zm['lane'])
console.log(zm['elon'])
console.log(zm['karen'])
console.log(zm['unknown'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment