1

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  

2 Answers 2

4

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, '"')));

Sign up to request clarification or add additional context in comments.

4 Comments

hi am facing an issue missing parentheses but is working fine in the console when i put it in my js it's not working all ))); this become green instead of white or ending parentheses
Thank You for Yor Effort and answer brother. its working fine in the Console When i add the same code to script its shows some Error
@abhikrishnan You are welcome. May be that error is because of some other part of your code. I won't be here for a while. You may ask a new question with the relevant error and necessary code to reproduce the error. Hopefully you will get an answer.
its ok brother I had done that in the below way which i posted as an answer if someone get same error means it will helpfull to them so. I posted it as an answer below. Thank YOu Verymuch for your Effort and Valuable Time. Thank YOu.
0

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+ "];")());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.