I want to convert the following string to an array
var string = '["YES","NO"]';
How do I do this?
I want to convert the following string to an array
var string = '["YES","NO"]';
How do I do this?
use the global JSON.parse method
JSON.parse('["YES","NO"]'); // returns ["YES", "NO"]
You can also use the JSON.stringify method to write the array back to a string if thats how you are storing it.
JSON.stringify(["YES", "NO"]); // returns '["YES", "NO"]'
var str= '["YES","NO"]';
var replace= str.replace(/[\[\]]/g,'');
var array = replace.split(',');
Fiddle : http://jsfiddle.net/9amstq41/
alert( "YES".length ); alert( array[0].length );var str= '["Are you [[sure?]]","No, not really"]';.filter(Boolean) after the split to remove empty string values.