Skip to content
Advertisement

Saving multiple data for a user JSON array

For a site I’m working on, a user has the option to input exercise data and it is saved to a JSON file in an array:

JavaScript

But what I want it to do is to be able to save it in a multidimensional array so multiple records can be put in for one user.

Example:

JavaScript

That way multiple records for the user are saved under one primary object to make it easier to access later. However I’ve never used multi-dimensional arrays before so I can’t figure out how to modify my code to get this to work.

JavaScript

Advertisement

Answer

If Email is the attribute to uniquely identify a user. It would be great to keep this as key of our array to easily find and add a new data to existing file.

Now the format we would be storing in file looks like

JavaScript

Now how to add new data to this file would be in following way.

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