How to convert array in double quotes back as original array in Javascript - Stack Overflow

How to convert this string into original array in Javascript?var str_arr = "["myName",&q

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

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];
Share Improve this question asked Dec 16, 2016 at 19:25 Umair JameelUmair Jameel 1,6733 gold badges31 silver badges58 bronze badges 4
  • 1 What do you mean by "original" array in your first sentence? – Neo Commented Dec 16, 2016 at 19:27
  • I mean, remove double quotes. – Umair Jameel Commented Dec 16, 2016 at 19:28
  • Possible duplicate of Safely turning a JSON string into an object – Андрей Беньковский Commented Dec 16, 2016 at 19:37
  • 1 Your first line is invalid JavaScript. You need to use single quotes or escape your inner double quotes. – trincot Commented Dec 16, 2016 at 22:24
Add a ment  | 

2 Answers 2

Reset to default 4

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744347191a4569755.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信