Converting a string of an array into an array in JS Converting a string of an array into an array in JS express express

Converting a string of an array into an array in JS


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

const response = "[1,2,3]";console.log(JSON.parse(response));


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


Depends on how you formed the string. If you used , for joining the elements, then you can use javascript's string.split() method.

let str = '1,2,3,4';let arr = str.split(',');

Just pass in whatever delimiter you used to join the elements.

OR

If you're saving elements as a json string, then use JSON.parse(str) as shown by Nils Kähler in his answer