简体   繁体   English

如何将日期数组的字符串表示形式转换为数组JS?

[英]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 我需要将数组的字符串表示形式转换为JS数组以循环使用,该字符串被单引号引起来

in My js 在我的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+ "];")());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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