Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert array in double quotes back as original array in Javascript

How to convert this string into original array in Javascript?

var str_arr = "["myName","asldkfjs","2342","[email protected]","sdlkfjskldf",410]"

like I want to store it back as original array

var arr = ["myName","asldkfjs","2342","[email protected]","sdlkfjskldf",410];
like image 723
Umair Jameel Avatar asked Oct 30 '25 09:10

Umair Jameel


2 Answers

You have a syntax error in your str_arr.

var str_arr = '["myName","asldkfjs","2342","[email protected]","sdlkfjskldf",410]';
var arr = JSON.parse(str_arr);
like image 132
manonthemat Avatar answered Oct 31 '25 23:10

manonthemat


You could try parsing it as JSON, because an array is valid JSON (assuming it uses double quotes for the strings).

arr = JSON.parse(str_arr);

Also, as @manonthemat mentioned, you need to either use single quotes to wrap the string literal where you declare str_arr (since it contains double quotes) or you need to escape the double quotes to avoid a syntax error.

like image 44
Justin Ryder Avatar answered Oct 31 '25 23:10

Justin Ryder



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!