1

I have a string like "[1,2,3]" and I want to convert it into array like [1,2,3] using JavaScript. Can anyone help me to do this?

4
  • Is your code snippet alright? It looks like nothing shows up when I run it. Commented Aug 9, 2020 at 19:04
  • 1
    Do you need those <br>? I don't see you are hidding them Commented Aug 9, 2020 at 19:12
  • 1
    it's because of all the br Commented Aug 9, 2020 at 19:12
  • 1
    use JSON.parse() Commented Sep 16, 2020 at 10:25

2 Answers 2

3

Since the string you want to convert is compatible to the JSON format (JavaScript object notation), you can use JSON.parse to convert it into an array:

const str = "[1,2,3]";
const arr = JSON.parse(str);
console.log(arr);

Sign up to request clarification or add additional context in comments.

1 Comment

Always glad to help!
1

Here you go

  var dat = "[1,2,3]";

  var myData = JSON.parse(dat);
  console.log(myData);

1 Comment

Nvm just saw the above answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.