Umair Jameel
Umair Jameel

Reputation: 1673

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];

Upvotes: 4

Views: 4231

Answers (2)

Justin Ryder
Justin Ryder

Reputation: 777

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.

Upvotes: 3

manonthemat
manonthemat

Reputation: 6251

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);

Upvotes: 4

Related Questions