Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I store a 2-dimensional array into cookies?

I’m trying to save my states into the cookies using the react-cookie package. It’s ok when I want to store some strings into the cookie but my problem is when it comes to saving arrays. For example, I have an array with the below structure:

const arr = [
      ['correct','correct','absent'],
      ['present','correct','present'],
      ['absent','absent','correct']
]

And now I want to store it into the cookie with the below code:

setCookie("states",arr);

When I tried to get states from the cookie with cookies.states I just got an object with correct,correct,absent,present,correct,present,absent,absent,correct result.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my question:
How can I store a 2-dimensional array into cookies and get it with array type?

>Solution :

If it works well with strings did you try to stringify it before saving ?

setCookie("states", JSON.stringify(arr));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading