但我必须将此请求转换为 json,如下所示:
{
"version":4.2,
"auth_token": "xxxxxxxx",
"zcontacts":[
{ "phone": "xxxxx", "name": "xxxx" },
{ "phone": "112", "name": "Distress Number" },
{ "phone": "1800-300-xxxx", "name": "UIDAI" },
{ "phone": "44 xxxxx, "name": "Ab zz" }
]
}
下面是我正在尝试的代码:
index(req_data) {
const self = this;
return new Promise((resolve, reject) => {
console.log(typeof req_data)
let req_body = {}
req_body.version = req_data.version;
req_body.auth_token = req_data.auth_token;
let contacts_list = [];
return Promise.each(req_data.zcontacts,(contact, key, length) => {
// Logic starts from here
if(key === 0) {
contacts_list.push({
phone: req_data.zcontacts[key],
name: req_data.zcontacts[(key+1)]
})
} else if(key > 0) {
contacts_list.push({
phone: req_data.zcontacts[(key+3)],
name: req_data.zcontacts[(key+4)]
})
}
}).then(() => {
req_body.zcontacts = contacts_list;
resolve(req_body)
});
谁能建议在循环下的代码中应该改变什么?