I have a string stored in a variable
clickedArray = '["0", "1", "2", "3"]'
This array is long and i have to extract the array inside this string into
array = ["0", "1", "2", "3"]
i then have to match every element of this array to a number which i will manage on my own. I am however struggling to extract this array from a string
I tried splitting the string but it yielded weird results and did not get what i expected.
CodePudding user response:
Parse the string as JSON:
const clickedArray = '["0", "1", "2", "3"]'
const array = JSON.parse(clickedArray)
console.log(array)
CodePudding user response:
You can use the parse function on JSON
lib string and convert it to array
clickedArray = '["0", "1", "2", "3"]';
let array = JSON.parse(clickedArray);
console.log(array); // ["0", "1", "2", "3"]
docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON