To parse it you need to use double quotes instead of single.
This should work:
services = '["service1", "service2", "service3"]'
JSON.parse(services)
Answer from Erick R Soto on Stack OverflowTo parse it you need to use double quotes instead of single.
This should work:
services = '["service1", "service2", "service3"]'
JSON.parse(services)
This is used to convert string which is array into pure array
var a = '[Aakash,akash]'
a.replace(/\[|\]/g,'').split(',')
(2) ["Aakash", "akash"]
javascript - Convert string with commas to array - Stack Overflow
help convert nested string array objects to array
How to convert Array formatted as String to Array?
Converting a String to an Array
Can we convert a string into a list instead of an array?
How to handle empty strings when converting to an array?
What is a string in Java?
For simple array members like that, you can use JSON.parse.
var array = JSON.parse("[" + string + "]");
This gives you an Array of numbers.
[0, 1]
If you use .split(), you'll end up with an Array of strings.
["0", "1"]
Just be aware that JSON.parse will limit you to the supported data types. If you need values like undefined or functions, you'd need to use eval(), or a JavaScript parser.
If you want to use .split(), but you also want an Array of Numbers, you could use Array.prototype.map, though you'd need to shim it for IE8 and lower or just write a traditional loop.
var array = string.split(",").map(Number);
Split it on the , character;
var string = "0,1";
var array = string.split(",");
alert(array[0]);
I have the following string:
str = [ "[1, 2]", "[2, 3]", "[4, 6]" ]
need nested arrays to be treated as arrays. Tried
for (var s of str) {
console.log(JSON.parse("[" + s + "]"));
// some operations over s here
// ...
}But I don't think this is a standard practice, any help in this matter is appreciated. Thanks
I'm given these strings of formatted arrays
"["cat","dog","horse"]"
"[1123,1234,2345]"
How do I format them into an array such that
arr[0] = "cat" arr[1] = "dog" arr[2] = "horse"
or
arr[0] = 1123 arr[1] = 1234 arr[2] = 2345
I have a string that is something like ["Value1","Value2","Value3"]. I would like to convert this to an array. I'd like to know if there is an easy way to convert that is effectively an array in String format into an actual array.
I'd prefer to avoid making a big loop that inserts the values from the string into a new array with find and substr but if that's required then that can be done.