Skip to content

Instantly share code, notes, and snippets.

@sscotth
Created September 18, 2019 15:51
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 sscotth/41a35ff3116f5c7cbd804459c33123d0 to your computer and use it in GitHub Desktop.
Save sscotth/41a35ff3116f5c7cbd804459c33123d0 to your computer and use it in GitHub Desktop.
JS Object.map
const obj = { foo: 'hello', bar: 'world' }
const updatedObj = Object.fromEntries(
Object.entries(obj).map(([key, val]) => [
key,
val.toUpperCase(),
]),
)
// {foo: "HELLO", bar: "WORLD"}
@sscotth
Copy link
Author

sscotth commented Sep 18, 2019

for the adventurous:

Object.map = (obj, cb) => Object.fromEntries(
  Object.entries(obj).map(([key, val]) => [
    key,
    cb(val),
  ]),
)

const obj = { foo: 'hello', bar: 'world' }

Object.map(obj, val => val.toUpperCase())

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