Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string of an array into an array in JS

I have a project I'm working on that implements MySQL, React and Express.js. I need to save an array into MySQL, but there are currently no ways to save an array, as far as I could see, so I was forced to convert it into a string. When I get it back from Express to the client, it's obviously a string, so I can't access the data. This array is used for a graph, mainly. What are some of the ways I can convert this string back to an array?

like image 220
DareRelaqz Avatar asked Apr 26 '26 12:04

DareRelaqz


2 Answers

You can use JSON.parse() to convert a string into an array.

const response = "[1,2,3]";

console.log(JSON.parse(response));
like image 75
Nils Kähler Avatar answered Apr 28 '26 03:04

Nils Kähler


You can store your json object (including arrays )in form of text in mysql database.What you have to do is JSON.stringify("your array") and persist it in database.And while you are retrieving it back from database you can JSON.parse() to get it in form of JavaScript object

like image 32
Shubh Avatar answered Apr 28 '26 02:04

Shubh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!