Skip to content

Instantly share code, notes, and snippets.

@crshmk
Created July 4, 2022 16:45
Show Gist options
  • Save crshmk/7a5ac549c970ece6ace0b960fe602f9f to your computer and use it in GitHub Desktop.
Save crshmk/7a5ac549c970ece6ace0b960fe602f9f to your computer and use it in GitHub Desktop.
a cleaner Object.entries api
const mapObj = (fn, obj) =>
Object.entries(obj).map(([key, value]) => {
return fn(value, key)
})
@crshmk
Copy link
Author

crshmk commented Jul 4, 2022

const obj = {
  one: 1,
  two: 2
}

const inc = x => x+1

mapObj(inc, obj)
// [2, 3]

@crshmk
Copy link
Author

crshmk commented Jul 4, 2022

const obj = {
  one: 1,
  two: 2
}

const makeOptions = (value, key) => ({ label: key, value })

mapObj(makeOptions, obj)
// [
//    { label: 'one', value: 1 }, 
//    { label: 'two', value: 2 } 
// ]

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