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.

anonimostilton
  • 170
  • 1
  • 16

1 Answers1

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);
Msk Satheesh
  • 1,398
  • 1
  • 6
  • 7