Skip to content

Instantly share code, notes, and snippets.

@mrcnc
Last active July 22, 2019 23:02
Show Gist options
  • Save mrcnc/4af359833af45447eef4d40512239ef0 to your computer and use it in GitHub Desktop.
Save mrcnc/4af359833af45447eef4d40512239ef0 to your computer and use it in GitHub Desktop.
turn browser cookies into an array of key/value pairs (in a functional style)
// https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
document.cookie.split(';')
.map(s => s.split('='))
.filter(a => a[0] && a[1])
// if there is a shorter, code golf way to do this, let me know
.reduce((acc, a) => { let obj = {}; obj[a[0].trim()] = a[1]; acc.push(obj); return acc }, [])
@mrcnc
Copy link
Author

mrcnc commented Jul 22, 2019

document.cookie.split(';')
  .map(c => c.trim().split('='))
  .filter(c => c[0] && c[1])
  .map(c => JSON.parse(`{"${c[0]}": "${c[1]}"}`))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment