I have a variable var s = '["hi","hello","What\'s new"]'; in string format.
How do i get the values hi hello What's new separately.
Also the values in the variable s is dynamic, some times it maybe var s = '["wow","okay"]'; or any values.
Can someone help me with this?
1 Answer
First of all, there is an error in this code var s = '["hi","hello","What's new"]' you need to write var s = '["hi","hello","What\'s new"]' otherwise it will give you this error:
Uncaught SyntaxError: Unexpected identifier
If you are looking for the output like this:
["hi","hello","What's new"]
Then this is the solution:
var s = '["hi","hello","What\'s new"]'
console.log(JSON.parse(s))
JSON.parse()to get an array froms, then use aforloop to iterate it and get the items - your question looks similar to this question: How to split string which is in the form an array using JavaScript?var s = '["hi","hello","What's new"]'you need to writevar s = '["hi","hello","What\'s new"]'