Javascript 如何删除postman中某个值末尾的空格,然后将响应保存在某个变量中?

Javascript 如何删除postman中某个值末尾的空格,然后将响应保存在某个变量中?,javascript,postman,Javascript,Postman,我有一个json响应,其中一些值的末尾有空格 我正在对一个API和另一个API进行响应比较,其中一个API具有正确的值,而另一个API在末尾具有空间,因此我的比较逻辑返回其不匹配。我想在比较中跳过这种不匹配 我只想在整个json响应中删除两个键中的空格 实际反应的一部分。请注意,它是一个数组响应,数组中有380个元素 [ { "term": 1, "monthlyCost": 15.0, "

我有一个json响应,其中一些值的末尾有空格

我正在对一个API和另一个API进行响应比较,其中一个API具有正确的值,而另一个API在末尾具有空间,因此我的比较逻辑返回其不匹配。我想在比较中跳过这种不匹配

我只想在整个json响应中删除两个键中的空格

实际反应的一部分。请注意,它是一个数组响应,数组中有380个元素

[
    {
        "term": 1,
        "monthlyCost": 15.0,
        "totalMinimumCost": 15.0,
        "shortDescription": "$10 International Value Pack ",
        "displayName": "$10 International Value Pack ",
        "longDescription": "Includes Infinite standard TXT here and to overseas.",
        "displayPriority": 4,
        "externalSkuId": "NA",
        "auxiliaryMedia": {
            "T&CContent": "Please read the terms and conditions <a href=\"http://URL/csFreeModule\"  target=\"_blank\">here</a>."
        },
        "inclusions": "Infinite national and international TXT",
        "parentProduct": "prod514df70mdl4",
        "id": "sku1347dfg0491mdl4"
    },
    {
        "term": 1,
        "monthlyCost": 30.0,
        "totalMinimumCost": 30.0,
        "shortDescription": "7GB Data Add-on",
        "displayName": "$30 7GB Data Add-on Month to Month",
        "longDescription": "Includes data to use on your mobile phone within country.\rAny additional data use will be charged as per the applicable additional data rate for your plan product – refer to your specific plan’s additional data charge rate for details. ",
        "displayPriority": 1,
        "externalSkuId": "AUD0450",
        "auxiliaryMedia": {
            "T&CContent": "<p><strong>Data Add-on $30 for 7GB</strong></p>\r<p>This Add-on costs $30 per month and includes 7GB of data (equals $0.0042/MB) to use in country.</p> <p>Only available to customers connected to eligible voice plans. Personal use only. Data expires after 1 month. Minimum monthly spend is $30. Recurring monthly access fee charged until the end of the month in which you cancel. Data deducted in per KB increments. Once you exhaust your Add-on’s Included Data, any further data usage will be deducted from your voice plan’s Included Data (if any). Thereafter, it will be charged at the additional data rate of your voice plan – refer to your plan for details. Add-on will be cancelled if there is no longer an active voice plan on your account. Min monthly spend is not prorated when Add-On is purchased or cancelled part way through the billing month. If provisioned on an account which supports data sharing, included data is shareable across the group.</p>\r \r\r<p>Companu Pty Ltd ABN 76 062 954 554.</p>\r</div>\r"
        },
        "inclusions": "7GB",
        "parentProduct": "prod5170040stgvha",
        "cisInfo": "http://URL/july-17.pdf",
        "id": "sku13511we376stgvha"
    }
]

我想删除“shortDescription”和“displayName”的“Pack”末尾的空格。之后,我需要将其作为JSON存储在某个变量中,以便与其他API进行比较。如何做到这一点?提前感谢

如果您的响应类型为string,请使用
JSON对其进行解析。parse
并为响应中的每个项目调用一个函数,该函数将修剪该对象中的字符串。你可以使用

const响应=[
{
任期:1,
月成本:15.0,
最低总成本:15.0,
简短描述:“$10国际价值包”,
displayName:“$10国际价值包”,
longDescription:“包括无限标准的TXT文件在这里和海外。”,
显示优先级:4,
外部SKUID:“不适用”,
辅助媒体:{
“T&C内容”:
“请阅读条款和条件。”,
},
包含:“无限国家和国际TXT”,
父产品:“prod514df70mdl4”,
id:“sku1347dfg0491mdl4”,
},
{
任期:1,
月成本:30.0,
总最低成本:30.0,
简短描述:“7GB数据加载项”,
displayName:“$30 7GB数据按月加载”,
详细说明:
“包括要在您所在国家/地区的移动电话上使用的数据。\r任何其他数据使用都将按照您的计划产品适用的附加数据费率收取费用–有关详细信息,请参阅您特定计划的附加数据费率。”,
显示优先级:1,
外部skuid:“AUD0450”,
辅助媒体:{
“T&C内容”:
“7GB的数据附加组件30美元

\r此附加组件每月成本30美元,包括7GB的数据(等于0.0042美元/MB)在国家/地区使用。

仅适用于连接到符合条件的语音计划的客户。仅供个人使用。数据在1个月后过期。每月最低花费为30美元。每月定期访问费收取,直至您取消的月末。数据以每KB的增量扣除。一旦您耗尽附加模块包含的数据,任何进一步的ata使用量将从您的语音计划包含的数据中扣除(如果有)。此后,它将按照您的语音计划的附加数据速率收费–有关详细信息,请参阅您的计划。如果您的帐户上不再有活动语音计划,则附加模块将被取消。购买附加模块或在计费月的一部分取消附加模块时,最小每月支出不会按比例计算。如果在支持rts数据共享,包括的数据可在整个集团内共享。

\r\r公司私人有限公司ABN 76 062 954 554。

\r\r“, }, 内含物:“7GB”, 父产品:“prod5170040stgvha”, cisInfo:“http://URL/july-17.pdf", id:“sku13511we376stgvha”, }, ]; 常量空间=(obj)=>{ Object.keys(obj.forEach)((key)=>{ 如果(对象的类型[键]=“字符串”)对象[键]=对象[键].trim(); }); }; 响应。forEach(trimSpaces);
控制台日志(响应)您可以使用此代码删除字符串开头和结尾的空格。但实际上,我的JSON响应非常大,它的结果数组也很大。我在一个变量中得到JSON响应,以及如何从这里修剪。我用下面的json变量尝试了上面的代码,但失败了。var json=json.parse(responseBody);由于API的JSON响应非常大,我必须将其存储在一个变量中。请在问题中发布JSON响应的某些部分,以便我能够理解结构并提供适当的答案。我添加了部分响应。请看一看。谢谢你的帮助help@Maddy请现在检查,如果它不工作,请告诉我。谢谢你这么多@Abito它工作得很好。这正是我想要的。
var responseData = JSON.stringify(responseBody);
var noSpacesString= responseData.replace(/\$10 International Value Pack /g,'$10 International Value Pack');;// remove space from end
console.log(JSON.parse(noSpacesString));