I have a string that contains an array with numbers in square bracket like this [1, 2,3, 4]. I want convert that string into array of integers. I can use split function but I also need to strip of square brackets and remove spaces if any between the numbers.
The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.
JavaScript Array toString() The toString() method returns a string with array values separated by commas.
if you already have the string like this "[1, 2,3, 4]"
JSON.parse will do
var arr = JSON.parse( "[1, 2,3, 4]" );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With