Skip to content
Advertisement

Export some properties from the nested object

Let’s say there is an object that looks like this:

JavaScript

Now I would like to export some of the nested values so that it can be used in various files across the app (exporting the entire object is not an option). What we could do is manually create all the required variables and export them like this:

JavaScript

The problem is that if we add more properties to this object (more cars or bikes), it will be very difficult to maintain as we have to manually create new variable and export them each time something new is added. Could you advice how this can be improved?

Advertisement

Answer

I suppose you could iterate through the object, construct a similar key for each vehicle/model, then export another object or array with that data.

JavaScript

or

JavaScript

or

JavaScript
Advertisement