struct - 一个键在多个相同类型的结构上的映射是否可能?
问题描述
我正在尝试将一个地址映射到属于同一地址的多个相同类型的结构上。如果我想在之后根据请求为一个地址选择任何“存储”结构,我该怎么做?
我创建了一个名为 Prescription 的结构,以及与患者地址的映射。所以我真正想要的是将患者地址映射到几个处方结构。
struct Prescription {
address patients_address;
string medicament;
string dosage_form;
uint amount;
uint date;
}
mapping (address => Prescription) ownerOfPrescription;
address [] public patients;
function createPrescription(address patients_address, string medicament, string dosage_form, uint amount, uint date) public restricted {
var newPrescription = ownerOfPrescription[patients_address];
newPrescription.medicament = medicament;
newPrescription.dosage_form = dosage_form;
newPrescription.amount = amount;
newPrescription.date = date;
patients.push(patients_address) -1;
}
function getPre(address _address)view public returns (string, string, uint, uint){
return(
ownerOfPrescription[_address].medicament,
ownerOfPrescription[_address].dosage_form,
ownerOfPrescription[_address].amount,
ownerOfPrescription[_address].date);
}
现在我有一个函数,我可以调用一个病人的所有书面处方。实际上,我只能为一个地址调用最后的书面处方。
解决方案
当然,a 的值类型mapping
可以是数组:
// map to an array
mapping (address => Prescription[]) ownerOfPrescription;
function createPrescription(...) ... {
// add to the end of the array
ownerOfPrescription[patients_address].push(Prescription({
medicament: medicament,
...
});
patients.push(patients_address);
}
推荐阅读
- javascript - NextJS GetStaticPath 创建重静态数据?
- node.js - 如何告诉 TypeORM 使用不同的记录器(在这种情况下为 pino)?
- sql - MS Access SQL 计算 dBµV 的对数平均值
- javascript - 处理多个请求时node.js中的ETIMEDOUT错误
- javascript - 子收藏更新触发云功能
- python-3.x - 从网络抓取中获取数字
- reactjs - React / Next.js 更改图像 src onLoad
- python - Python如何格式化请求数据
- python-3.x - 如果我事先不知道将选择哪个序列化程序,如何动态序列化字段?
- javascript - 如何将活动类添加到视口部分“data-nav”中显示的列表“href”中