简体   繁体   中英

How convert a string representation of date array to array JS?

I need to convert a string representation of array to JS array for looping purpose the string is single quoted

in My js

var length1 = $('.length').text();  //['2018-9-24', '2018-9-26', '2018-9-25']
console.log(length1.length) // 39 as output i need it as 3 

to loop through each date

Any help would be appreciated

I tried

var myArray=json.parse(length1) // but its not working  

Replace single quotes with double and then parse it:

 var str = "['2018-9-24', '2018-9-26', '2018-9-25']"; console.log(JSON.parse(str.replace(/'/g, '"'))); 

I had done this in an another way

var objectstring = "['2018-9-24', '2018-9-26', '2018-9-25']";
var objectStringArray = (new Function("return [" + objectstring+ "];")());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM