1

I would like to convert string formatted text into an array as follows. Source string is like:

var myString = "[3, 8750, "Some text, with commas"]";

Required result is as follows:

myArray[0] = 3;
myArray[1] = 8750;
myArray[2] = "Some text, with commas";

I will appreciate any kind of support..

6
  • Looks like it's just JSON. So use JSON.Parse? Commented Jun 4, 2018 at 21:56
  • That is not a valid string. Check your quotes and correct please. Commented Jun 4, 2018 at 21:56
  • 1
    JSON.parse(myString), but fix your string format first (ex: make the outer quotes single). Commented Jun 4, 2018 at 21:56
  • Possible duplicate of How can I parse a CSV string with Javascript, which contains comma in data? Commented Jun 4, 2018 at 22:04
  • On the backend side, I didn't defined it as JSON, but an ordinary array.. Anyway thanks for your quick support Commented Jun 4, 2018 at 22:10

1 Answer 1

1

Use JSON.parse() to change string formated value into to array.

var myString = "[3, 8750, \"Some text, with commas\"]";
let myStringList =JSON.parse(myString);
console.log(myStringList);

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

Comments

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.