Skip to content

Instantly share code, notes, and snippets.

@snekse
Created December 18, 2018 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snekse/df2c01f1134ea98d68415b8c4e9885d0 to your computer and use it in GitHub Desktop.
Save snekse/df2c01f1134ea98d68415b8c4e9885d0 to your computer and use it in GitHub Desktop.
Simple function to take an object with a bunch of `data-` attributes and convert to a new object that has keys with the `data-` stripped
const convertDataAttributes = dataAttributes =>
Object.entries(dataAttributes)
.filter(([k, v]) => k.startsWith("data-"))
.reduce((acc, [k, v]) => {
const newKey = k.slice(5); //`data-` length
acc[newKey] = v;
return acc;
}, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment