Here are some code examples for ES5, ES2015 and ES2017+. They all use the
reduce methods available on arrays. These two methods allow to separate the mapping logic from the object construction.
For each example, the goal is to transform this object:
To this one:
In the future
A new stage-0 proposal has been presented to add
Object.fromEntries, this could provide us a new way to write the last example:
And it would be even better with the pipeline operator (stage-1) to keep the method chaining: