prefer-object-from-entries
See original GitHub issuePrefer Object.fromEntries()
over Array#reduce()
unicorn/prefer-object-from-entries
const demo= [{a: 1 b:3, c: 3},{a: 11 b:13, c: 13}]
const reduced = demo.reduce((acc, item) => {
acc[item.a] = item.b
return acc
}, {})
Issue Analytics
- State:
- Created 2 years ago
- Comments:8
Top Results From Across the Web
eslint-plugin-unicorn/prefer-object-from-entries.md at main
Prefer using Object.fromEntries(…) to transform a list of key-value pairs into an object. This rule is enabled in the ✓ recommended config.
Read more >Object.fromEntries() - JavaScript - MDN Web Docs
The method returns an iterator object that produces two-element array-like objects. The first element is a value that will be used as a...
Read more >JavaScript Object.fromEntries() | SamanthaMing.com
Object.fromEntries is the inverse of Object.entries. It will take key-value pairs and return a new object. You can use it on Arrays and...
Read more >prefer-object-from-entries #1464 - Issuehunt
What is the problem? ... So, you are going to really suggest creating a new array just to have key/value pairs instead of...
Read more >Benchmark: Object.fromEntries vs reduce - MeasureThat.net
fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects) ... AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Better yet, no functions at all:
.reduce()
is mostly used as a generic loop. There’s nothing special in this case that justifies its awkward API.Perf test
I understand that there might be sight performance loss, but the second one is more readable to me.
I think this is acceptable.
I don’t think there are not too many cases in real world that the array is already key-value pairs, most of them need to transform. Any real word cases can help us to understand why you don’t like it?
The one in the topic seems silly to me, if there are duplicated value of
a
property, it may result unexpected.