Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform a string to an array

Tags:

javascript

How to transform a string of this format '[11, 66]' to an array ?

Array.isArray('[11, 66]') return false since it's considered as string?

like image 314
user2222 Avatar asked Aug 15 '26 02:08

user2222


2 Answers

A good way to do this would be JSON.parse.

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

In your example:

JSON.parse('[11, 66]')

Output:

[11, 66]
like image 103
Cam Avatar answered Aug 16 '26 17:08

Cam


If the string is a valid JSON string, then use JSON.parse:

var array = JSON.parse(str);
like image 37
ibrahim mahrir Avatar answered Aug 16 '26 15:08

ibrahim mahrir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!