Reputation: 836
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
Upvotes: 1
Views: 78
Reputation: 836
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+ "];")());
Upvotes: 0
Reputation: 39382
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, '"')));
Upvotes: 4