beano
beano

Reputation: 952

Tips on storing 2D arrays

I have a 2 dimensional array like: [1,[a,b,c,d,e],2,3]

I have it stored in an SQL database and am trying to retrieve it in the same format.

However it currently comes out as: 1,a,b,c,d,e,2,3

Any tips on how to get it back to its original 2D array format?

If not, are there any tips on how to better store the original array (maybe with different separators) so it is easier to split on the way out.

Sorry if this is a silly question. I am new to SQL databases and storing arrays.

Upvotes: 1

Views: 133

Answers (2)

Anastasios Selmani
Anastasios Selmani

Reputation: 3699

The second array should probably be another table and the second field should be a reference to that table.

Table A Row1 : id | 1 | table2Id | 3 | 4

Table B Row1 : id | a | b | c | d

Upvotes: 3

Nina Scholz
Nina Scholz

Reputation: 386568

It looks like that some part (of datatabase or mapper) is a using toString performing for every array. After formatting, the arrays are gone.

You might consider to use a JSON data format and convert the array first with JSON.stringify and store the plain string in the database.

After fetching from the database, you could convert it back to an array or object with JSON.parse.

Upvotes: 1

Related Questions