Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript convert string into array of int with brackets [duplicate]

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.

like image 451
Jazz Avatar asked Feb 12 '16 06:02

Jazz


People also ask

How to convert string type to array in JavaScript?

The string in JavaScript can be converted into a character array by using the split() and Array. from() functions.

Can we convert array to string in JavaScript?

JavaScript Array toString() The toString() method returns a string with array values separated by commas.


1 Answers

if you already have the string like this "[1, 2,3, 4]" JSON.parse will do

var arr = JSON.parse( "[1, 2,3, 4]" );
like image 78
gurvinder372 Avatar answered Oct 08 '22 18:10

gurvinder372