Home > Net >  How do I remove an array from a string?
How do I remove an array from a string?

Time:04-30

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

CodePudding user response:

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);

CodePudding user response:

You can parse to get the desired solution

JSON.parse(location)

CodePudding user response:

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));
  • Related