0

I have a list detected as a string like this one:

var list = "['one', 'two', 'three']"

Is there a way to turn this into an array:

var real_list = ['one', 'two', 'three']

Edit: But I can't change the quotation marks of var list.

0

1 Answer 1

1

Try Using

var list = "['one', 'two', 'three']";
list = list.split(",");
list[0] = list[0].substring(1);
list[list.length - 1] = list[list.length - 1].substring(
  0,
  list[list.length - 1].length - 1
);
list.forEach((x, i) => {
  list[i] = list[i].includes('"') ? list[i].replaceAll('"', "").trim()
     : list[i].replaceAll("'", "").trim();
});

console.log(list);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.