Skip to content
Advertisement

JAVASCRIPT DICTIONARY CREATION DYNAMICALLY

I am back-end developer , and i had to work with Frot-end a little ,but i am stuck in one problem.I am getting data from api and want my data to be stored in this way:

JavaScript

Api call looks like this :

JavaScript

enter image description here

So , i tried to use map and dict function like this to make dict like above ,but did not work:

JavaScript

But it does not add up to the existing dict instead gives me back only last fetched information,

Can anyone help with this?

Advertisement

Answer

Not sure what you are trying to achieve, but it looks like you get an array of objects from the backend and you want to turn it into an array of objects, but with different property names. That’s a simple Array.prototype.map():

JavaScript

If you are just trying to create some kind of centralized state manager / store that would also implement accessors to that data, then the main issue is you are passing properties one by one; instead, you should just call add once per item (question):

JavaScript
JavaScript

However, I think that’s not the case and this might just be some kind of confusion.

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