I have..
var genre = ["M","F"];
without single quotes and need
["M", "F"]
Not
M,F
With quotes and [ ]; Any idea?
Thanks.
I have..
var genre = ["M","F"];
without single quotes and need
["M", "F"]
Not
M,F
With quotes and [ ]; Any idea?
Thanks.
function strToArr( str ) {
var arr = [];
if( typeof str === 'string' ) {
str = str.substring( 1, str.length - 1 );
arr = str.split( ',' );
}
return arr;
}
I don't how many will like it, but try
var genre = '["M","F"]';
genre = eval(genre )
If you don't like to use eval() then use @YuanZhaohao's answer
If I understood, this will do it:
var str = '["a","b","c"]';
var arr = str.split(/\"]|\["|\","/g).slice(1,-1);
You end up with an array like ["a", "b", "c"]
Note: you might want to do extra checks in case of the possibility of unexpected format like (e.g. with some random spaces '["a", "b" ,"c"]'
)
If you want a string, try this:
genre = genre.reduce(function(a, b) { return a + '' + b; });