News for hackers. Stuff that matters.
Re:ECMAScript 2017 Language Specification by HugoDaniel on Tuesday July 11, 2017 @11:33AM (#14745015)
Not really object map. Does not return a new object ;)
Re:ECMAScript 2017 Language Specification by thomasfoster96 on Tuesday July 11, 2017 @11:41AM (#14745077)
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 :)
Re:ECMAScript 2017 Language Specification by djfm on Tuesday July 11, 2017 @01:46PM (#14746238)
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) }) ) );

Re:ECMAScript 2017 Language Specification by komali2 on Tuesday July 11, 2017 @02:53PM (#14746919)
>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.

Re:ECMAScript 2017 Language Specification by djfm on Tuesday July 11, 2017 @05:28PM (#14748243)
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.

Re:ECMAScript 2017 Language Specification by rmrfrmrf on Tuesday July 11, 2017 @11:08PM (#14749880)
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.

Re:ECMAScript 2017 Language Specification by water42 on Tuesday July 11, 2017 @03:15PM (#14747105)
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
Re:ECMAScript 2017 Language Specification by tracker1 on Tuesday July 11, 2017 @06:16PM (#14748564)
I really like that... I've tended to use Object.assign in a reducer, but this works too, and is a little cleaner imho
Re:ECMAScript 2017 Language Specification by thomasfoster96 on Tuesday July 11, 2017 @11:23PM (#14749945)
Yep, it should be. Thanks for catching it.
Re:ECMAScript 2017 Language Specification by nathancahill on Tuesday July 11, 2017 @12:24PM (#14745458)
Your linter will also complain about modifying newObj within the reducer.
Re:ECMAScript 2017 Language Specification by paulddraper on Tuesday July 11, 2017 @12:38PM (#14745605)
Which a problem with using linters, not with programming.
Re:ECMAScript 2017 Language Specification by mhluongo on Tuesday July 11, 2017 @12:29PM (#14745518)
Spread syntax should cover linter issues

  Object.entries(obj).map(...).reduce((acc, [prop, value]) => ({...acc, prop:value}))
Re:ECMAScript 2017 Language Specification by Rotten194 on Tuesday July 11, 2017 @12:39PM (#14745620)
isn't that hideously inefficient? you're making a new object each iteration.
Re:ECMAScript 2017 Language Specification by Touche on Tuesday July 11, 2017 @12:58PM (#14745792)
But purity.
Re:ECMAScript 2017 Language Specification by djfm on Tuesday July 11, 2017 @01:42PM (#14746193)
it's very unlikely this is going to be the bottleneck in your application
Re:ECMAScript 2017 Language Specification by mintplant on Tuesday July 11, 2017 @02:53PM (#14746926)
I wouldn't be so quick to write that off as a performance concern. Creating tons of unnecessary objects is JavaScript's equivalent to programming without regard for cache locality at a lower level, due to the way the JITs work.
Re:ECMAScript 2017 Language Specification by Touche on Tuesday July 11, 2017 @03:11PM (#14747075)
It's very unlikely you'll get any benefit from doing it the inefficient way either. Does eslint not support "ignore this part" comments?
Re:ECMAScript 2017 Language Specification by Prefinem on Tuesday July 11, 2017 @06:22PM (#14748596)
Yes it does

// eslint-disable-line your-rule-to-ignore

Re:ECMAScript 2017 Language Specification by tracker1 on Tuesday July 11, 2017 @06:13PM (#14748543)
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.
Re:ECMAScript 2017 Language Specification by v413 on Wednesday July 12, 2017 @03:23AM (#14750752)
why not just use acc[prop] = value ?
Re:ECMAScript 2017 Language Specification by elmigranto on Wednesday July 12, 2017 @09:33AM (#14752354)
Assignment does not return `acc`.
Re:ECMAScript 2017 Language Specification by nawitus on Tuesday July 11, 2017 @01:18PM (#14745976)
Which linter rule is that?
Re:ECMAScript 2017 Language Specification by mirekrusin on Tuesday July 11, 2017 @01:35PM (#14746134)
The one that doesn't like modifying function arguments?
Re:ECMAScript 2017 Language Specification by nawitus on Tuesday July 11, 2017 @04:38PM (#14747838)
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.
Re:ECMAScript 2017 Language Specification by xanderjanz on Tuesday July 11, 2017 @02:52PM (#14746912)
Even in es5 you can do Object.keys().reduce().