Hacker News new | ask | show | jobs
by thomasfoster96 3263 days ago
Is Object.entries(obj).map((prop, value) => ...) close enough? Object.entries is newly standard in ES8.
2 comments

Not really object map. Does not return a new object ;)
Ah, well this would return a new object:

    Object.entries(obj).map((prop, value) => ...).reduce((newOdj, [prop, value]) => newObj[prop] = value);
I see your point about verbosity :)
Shouldn't it be ".map(([prop, value]) => ...)" ?

I like to write it this way, using as much syntax sugar as possible:

const mapObject = fn => obj => Object.assign( ...Object.entries(obj).map( ([key, value]) => ({ [key]: fn(value) }) ) );

>using as much syntax sugar as possible

It's not possible to say this on the internet without being rude so, apologies, but, why code like that? It genuinely was a struggle to parse (in my brain) your oneliner of code there. If I found this in our code base it would be a huge waste of time and energy.

No offense taken.

I said I like to write it this way, not that I always do it.

> why code like that

It's a fun exercise.

> it would be a huge waste of time and energy

I don't fully agree on this: from the name and signature it's pretty clear what the function does, so you don't need to waste time "parsing" the details. And with unit tests it is also very low-risk.

But that's speculative, in a real project I'd just use lodash.

In production code, if I ever have clever one-liners, they're usually offset with just as many additional lines of comments to explain what it's doing and why it works. Plus, this function is generic and would probably be extracted into a utility module, anyway.

To your point, though, this one is pretty esoteric.

the trend of style over readability. google recommends avoiding list comprehensions in python yet 99% of stackoverflow python questions have some convoluted list comprehension answer
I really like that... I've tended to use Object.assign in a reducer, but this works too, and is a little cleaner imho
Yep, it should be. Thanks for catching it.
Your linter will also complain about modifying newObj within the reducer.
Which a problem with using linters, not with programming.
Spread syntax should cover linter issues

  Object.entries(obj).map(...).reduce((acc, [prop, value]) => ({...acc, prop:value}))
isn't that hideously inefficient? you're making a new object each iteration.
But purity.
it's very unlikely this is going to be the bottleneck in your application
In those cases, I usually use

    Object.assign(acc, { [prop]:value })
While not sure if `[prop]` is better or worse than not creating a new interstitial object.
why not just use acc[prop] = value ?
Which linter rule is that?
The one that doesn't like modifying function arguments?
Usually that only disallows reassignment of function arguments. For example, eslint's no-param-reassign rule by default allows modifying the properties of the function argument. However, the "props" parameter for this rule can disallow even the modification of properties of a function's argument.
Even in es5 you can do Object.keys().reduce().
Object.assign({},obj)