我在这里遇到一个问题,我正在使用 Angular 5 和 ionic v.2 制作 ionic 应用程序
我有一个后端,可以帮助我通过单击按钮触发电子邮件,但在该电子邮件中,我必须从前端发布数据,也就是说通过电子邮件发送订单摘要。所以这个订单摘要是我从前端发布的。
我有一个带有键值对的 javascript 对象,我想将此 javascript 对象作为纯文本发布,这样当用户阅读他的电子邮件时,响应不应该是 json,而应该只是纯文本
作为正文发布在 api 中的对象示例:
coalCredentials = {
"company_id": "hitachi09",
"company_name": "hitachi" ,
"country": "" ,
"current_stock" : "88",
"delivery_remarks":"yes",
"demand_stock":"66",
"expected_order_delivery":"2018-08-08",
"gar":"",
"gsv":"",
"nar":"",
"plant_address":"gachibowli",
"product_name":"coal",
"product_size":"",
"product_type":"petcoke",
"seal_check":"yesonseal",
"vessel_type":"huit"
}
所以这是一个json格式的发送数据。我想以纯文本形式发送它,以便发送时的结果看起来是
company_id: hitachi09
company_name: hitachi
country:
current_stock :88
delivery_remarks:yes
demand_stock:66
expected_order_delivery:2018-08-08
gar:""
gsv:""
nar:""
plant_address:gachibowli
product_name:coal
product_size:""
product_type:petcoke
seal_check:yesonseal
vessel_type:huit
请有人帮助我们完成需要做的事情,以便电子邮件对客户来说看起来有意义。
更改应该在后端还是前端
前端采用 Angular 5,后端采用 Node.js
最佳答案
类似这样的吗?
const coalCredentials = {
"company_id": "hitachi09",
"company_name": "hitachi",
"country": "",
"current_stock": "88",
"delivery_remarks": "yes",
"demand_stock": "66",
"expected_order_delivery": "2018-08-08",
"gar": "",
"gsv": "",
"nar": "",
"plant_address": "gachibowli",
"product_name": "coal",
"product_size": "",
"product_type": "petcoke",
"seal_check": "yesonseal",
"vessel_type": "huit"
};
const textFormat = Object.entries(coalCredentials)
.map(([key, value]) => key + ':' + value)
.join('\n');
console.log(textFormat);
注意:如果您使用的是 Windows,请使用 \r\n
而不是 \n
,或者 this如果您需要特定于平台的。
替代版本,不需要Object.entries
(仅从 ES2017 开始存在):
const coalCredentials = {
"company_id": "hitachi09",
"company_name": "hitachi",
"country": "",
"current_stock": "88",
"delivery_remarks": "yes",
"demand_stock": "66",
"expected_order_delivery": "2018-08-08",
"gar": "",
"gsv": "",
"nar": "",
"plant_address": "gachibowli",
"product_name": "coal",
"product_size": "",
"product_type": "petcoke",
"seal_check": "yesonseal",
"vessel_type": "huit"
};
const textFormat = Object.keys(coalCredentials)
.map(key => key + ':' + coalCredentials[key])
.join('\n');
console.log(textFormat);
关于javascript - 如何将 javascript 对象转换为纯文本,以便在发布数据时我可以发布简单文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52677532/