在下面,我向扩散运算符添加了PatientId和inusranceId,只是想看看是否有更好的方法来完成此任务?
main.js
const Response = await getData(re);
const patientId = "AW364747";
const insuranceId = "0000786622";
logger().info('Response=', { ...Response, drugName: '[REDACTED]' , patientId, insuranceId});
答案 0 :(得分:3)
您所拥有的是正确的。
let newResponse = {...Response, drugName: '[REDACTED]'}
如果响应中缺少drugName属性,则将其添加。或者,如果它在Response中,它将覆盖drugName属性。
let newResponse = { drugName: 'defaultname', ...Response}
则相反,如果Response包含drugName,它将覆盖drugName:'defualtname';如果Response中不存在它,则将添加drugName。
示例:
const Response = { prop: 'some prop', drugName: 'Original Drug'}
const spreadFirst = { ...Response, drugName: 'New drug'}
// spreadFirst = { prop: 'some prop', drugName: 'New drug'}
const spreadLast = { drugName: 'Newest Drug', ...Response}
// spreadLast = { prop: 'some prop', drugName: 'Original Drug'}
答案 1 :(得分:0)
有更清晰直接的方法
let Response = { name: 'John' }; // await getData(re);
Response.drugName = '[REDACTED]';
Response.patientId = 'AW364747';
Response.insuranceId = '0000786622';
console.log(Response)