Skip to content
Advertisement

How to invert a nested object of objects

I’d like to learn how to convert a nested object into a new one by rearranging this structure:

JavaScript

So that it follows this structure:

JavaScript

Thanks

Advertisement

Answer

You can grab the entires of your object and use .reduce() to build a new object from the [[key, value], ...] entry array. For each [key, value] you can iterate over the entries of the nested object and use the keys from that as your outer keys of your accumulated/new object:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement