英文:
Getting Triple quotes in javascript
问题
Android移动应用程序正在使用请求数据发送到Nodejs API的方式发送正文,如下所示:
{
"openingHour":['"02:00PM"','"03:00PM"']
}
而在后端系统中,我无法仅使用JS删除单引号或双引号。
我的要求是:
{
"openingHour":["02:00PM","03:00PM"]
}
或
{
"openingHour":['02:00PM','03:00PM']
}
如何实现上述要求?
英文:
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.
答案1
得分: 0
你可以使用substring函数来删除每个条目的前导和尾随引号,并构建一个新的数组。
使用map函数,您可以迭代openingHour
数组中的每个元素,应用该表达式,并使用从map
返回的数组将其重新分配回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)
英文:
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
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论