Converting Array Inside A String To An Array
I have a response which contains an array which is present inside string(''). What I need I just the array. How do I get rid of the quotes? I tried JSON.parse(). It gave me error m
Solution 1:
Below solution will work for your case provided the strings does not contain /'
var a = "['abc@mail.com', 'cde@mail.com', 'def@mail.com']";
a = a.replace(/'/g, '"');
var result = JSON.parse(a);
Considering its an email data, there's no possibility of having that escape character sequence
Solution 2:
You have to send in backend the format with double quotes instead of one quotes or in front just replace quotes '
with double quotes " inside your array , otherwise the parse will fail ,
see below snippet :
let json = {
email_id: "['abc@mail.com', 'cde@mail.com']"
}
json.email_id = json.email_id.replace(/'/g,"\"");
console.log(JSON.parse(json.email_id));
Post a Comment for "Converting Array Inside A String To An Array"