0

I am trying to use associative arrays with redux and to store them in the async storage to retrieve on loading the app.

With redux it works very well I have real quick access to the values and can map the content very well into cards in the react native app.

Now when I store the data in async storage I have to JSON.stringify the content and JSON.parse it back on loading. Unfortunately somehow I cannot use the object the same way afterwards.

my data looks like this:

['1442-3338': { ID: '1442-3338', something: 'something', Participants: [] }, '1442-3339': { ID: '1442-3339', someting: 'something', secondArray: {} }, '1442-3340': { ID: '1442-3340', something: 'something', secondArray: {} }, '1442-3341': { ID: '1442-3341', something: 'something', secondArray: {} } ]

I can access the data via object['1442-3338'], then after storing into the storage and getting it back, which includes JSON.stringify on store and JSON.parse on load, I cannot read the data like that anymore - keys are not recognized.

Fapi
  • 323
  • 2
  • 15
  • Note: we don't actually call maps associative arrays in JavaScript. You either have objects (like in your code) or `Map` instances. In the case of objects in your code we just call them "objects". – Benjamin Gruenbaum Feb 18 '22 at 11:40
  • ` I cannot read the data like that anymore - keys are not recognized.` - without a fully reproducing example it is very hard to help you debug this and I doubt you'll get much useful info. I would consider including a reproducible example. – Benjamin Gruenbaum Feb 18 '22 at 11:40
  • okay thanks will do a little later and sure we can call it "object" :) – Fapi Feb 18 '22 at 11:52

1 Answers1

0

I found an answer already here on stackoverflow.

JSON does not allow named keys in arrays. Thus on transforming the data via JSON.stringify and JSON.parse the array lost the keys.

Had to use a "normal" object instead, which now works fine.

Link to explanation in another post

Fapi
  • 323
  • 2
  • 15