'How do I remove an array from a string?

Here is the code

location: '["41.8481","-72.0345"]'

Here is what I tried

location.replace(/'/g,'');

This is what I want

location: ["41.8481","-72.0345"]

Any help would be appreciated



Solution 1:[1]

You can parse the string to an array using JSON.parse

const loc = '["41.8481","-72.0345"]';
const locArr = JSON.parse(loc);
console.log(locArr);

Solution 2:[2]

You can parse to get the desired solution

JSON.parse(location)

Solution 3:[3]

first off, i have no idea why you might do this. if you want to edit an array and put it into a string, and then get the array again, just split it with a character. one thing that you could do is this:

let hello = "a|b|c|d|e|f|g";
console.log(hello.split("|")[0]);
//result: "a"

or use JSON.parse. it's easy as 1, 2,

console.log(JSON.parse(location));

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 Anonymous Panda
Solution 2 dinesh oz
Solution 3 xxxxxxxxxx