我在这里遇到一个问题,我正在使用角度5和离子v.2进行离子应用
我有一个后端,可帮助我单击按钮触发电子邮件,但是在该电子邮件中,我必须从前端发布数据,即可以说订单摘要与电子邮件。因此,此订单摘要是从前端发布的。
我有一个具有键值对的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
所以请有人帮助您完成需要做的事情,以便使电子邮件对客户而言完全有意义。
更改应在后端还是在前端
前端位于5号角,后端位于node.js
答案 0 :(得分:3)
像这样吗?
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);
答案 1 :(得分:0)
let keyArr = Object.keys(coalCredentials);
let p = 0;
let final = "";
for(let x in coalCredentials){
final += keyArr[p] + ":" + x + "\n";
p++;
}
然后,您可以根据需要使用最终字符串。
答案 2 :(得分:0)
尝试在ES6中使用Object.keys
,希望对您有所帮助!
var 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"
}
var plainText = '';
var keyArray = Object.keys(coalCredentials)
var valueArray = Object.keys(coalCredentials).map(key => coalCredentials[key])
for(let i=0; i<keyArray.length; i++) {
plainText = plainText + keyArray[i] + ': ' + valueArray[i] + '\n';
}
console.log(plainText)