Created
December 18, 2018 17:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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