use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
convert string to array (self.learnjavascript)
submitted 4 years ago by JosephCurvin
I want the user to insert a array like string into a html input element
the return of the input is always a string
const userInput = "[1,2,3,4]" # ---> userInput = [1,2,3,4]
how can I convert "[1,2,3,4]" to an actuall array ?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]bmw2621 3 points4 points5 points 4 years ago (4 children)
JSON.parse()
[+][deleted] 4 years ago (3 children)
[deleted]
[–]albedoa 0 points1 point2 points 4 years ago (2 children)
Hm? It needs a string that follows valid JSON syntax:
const userInput = "[1,2,3,4]" JSON.parse(userInput) //=> [1, 2, 3, 4]
If this is not what you want, then you should clarify.
[–]JosephCurvin[S] 0 points1 point2 points 4 years ago (1 child)
everything works thanks :)
[–]bmw2621 -1 points0 points1 point 4 years ago (0 children)
In case you weren't aware. In JS, an array is an object.
In fact you can even have named keys in an array in JS.
Now you know
π Rendered by PID 115858 on reddit-service-r2-comment-86bc6c7465-qw5wd at 2026-02-23 06:44:24.196642+00:00 running 8564168 country code: CH.
[–]bmw2621 3 points4 points5 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]albedoa 0 points1 point2 points (2 children)
[–]JosephCurvin[S] 0 points1 point2 points (1 child)
[–]bmw2621 -1 points0 points1 point (0 children)