0

I have a python script that spews a stringified Set of strings:

let mySet = "{\"apple\",\"pear\",\"berry\"}"

How can I parse it back into a Set using javascript?

I tried:

eval(mySet)
new Set(mySet)
asiera
  • 492
  • 5
  • 12

1 Answers1

0

Try this:

      let str = '{\\"apple\\",\\"pear\\",\\"berry\\"}';
      let arr = str.match(/[a-zA-Z]*/g).filter((item) => item);
      const newset = new Set([...arr]);
      console.log(newset);
user2495207
  • 861
  • 2
  • 10
  • 15
  • that wouldn't be very robust as it won't work if the string inside the Set contains number or special chars – asiera May 27 '22 at 08:51
  • @asiera Yeah but, I think it can be easly adapted changing the expression, `[a-zA-Z0-9_.-]` for example. – user2495207 May 27 '22 at 15:10