'How to convert a list of tuple that is a string into an Array in Javascript [closed]

I have a list of tuple in string format.

let tuple_list= '[("AB", "CD"), ("EF", "GH"), ("IJ", "KL")]'

I want it to convert in an array.

("AB", "CD" , "EF", "GH", "IJ", "KL")

Please let me know how can I do that.



Solution 1:[1]

let parsed = '[("AB", "CD"), ("EF", "GH"), ("IJ", "KL")]';

function parseTuple(t) {
    return JSON.parse(t.replace(/\(/g, "").replace(/\)/g, ""));
}

var result = parseTuple(parsed);
console.log(result);

Solution 2:[2]

Try this:

JSON.parse('[("AB", "CD"), ("EF", "GH"), ("IJ", "KL")]'.replaceAll("(", "[").replaceAll(")", "]")).flat()

Solution 3:[3]

JavaScript does not support tuples so you've to convert it into array so you can do like this

let tuple_list = '[("AB", "CD"), ("EF", "GH"), ("IJ", "KL")]'
tuple_list = tuple_list.replaceAll("(","").replaceAll(")","")
let result = JSON.parse(tuple_list)
console.log(result)

Andif you want nested array do like this

let tuple_list = JSON.parse('[("AB", "CD"), ("EF", "GH"), ("IJ", "KL")]'.replaceAll("(", "[").replaceAll(")", "]"))

console.log(tuple_list)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Solomon
Solution 2 max li
Solution 3