Skip to content

Instantly share code, notes, and snippets.

@sscotth

sscotth/objectmap.js

Created Sep 18, 2019
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@sscotth 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
You can’t perform that action at this time.