在JavaScript中获取三重引号

huangapple go评论70阅读模式
英文:

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 = {
  &quot;openingHour&quot;: [&#39;&quot;02:00PM&quot;&#39;, &#39;&quot;03:00PM&quot;&#39;]
}

console.log(&#39;before&#39;, json)

let newOpeningHourArray = json.openingHour.map(time =&gt;
  time.substring(1, time.length - 1))

json.openingHour = newOpeningHourArray

console.log(&#39;after&#39;, json)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月7日 01:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75034507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定