Home > Back-end >  Getting Triple quotes in javascript
Getting Triple quotes in javascript

Time:01-09

Android mobile app is sending the body in a API of Nodejs with the request data as

{ 
    "openingHour":['"02:00PM"','"03:00PM"']
}

and in the backend system I am unable to remove either single quote or double quote using JS only.

my requirement is

{ 
    "openingHour":["02:00PM","03:00PM"]
}

OR 

{ 
    "openingHour":['02:00PM','03:00PM']
}

how can i achieve the above requirements.

CodePudding user response:

You can use the substring function to remove the leading and trailing quote from each entry and build a new array out of it.

Using the map function you can iterate over each element in the openingHour array, applying that expression, and use the array returned from map to assign it back to the json

let json = {
  "openingHour": ['"02:00PM"', '"03:00PM"']
}

console.log('before', json)

let newOpeningHourArray = json.openingHour.map(time =>
  time.substring(1, time.length - 1))

json.openingHour = newOpeningHourArray

console.log('after', json)

  • Related