0

I have a Map of a List. I am managing the data in the map and the list. I have it working for now, but wanted to find out if there is a better way to do this.

mapping (bytes32 => Receipts[]) receiptsByPersonMap;
Receipts[] dummyReceiptsList;

Receipts newReceipt = new Receipts(_person, _cost);

dummyReceiptsList = receiptsByPersonMap[_person];

dummyReceiptsList.push(newReceipt);
receiptsByPersonMap[_person] = dummyReceiptsList;

This works for now. it seems that the last line is irrelevant - if receiptsList is pointing to the same storage space. Any suggestion of a better way to do this.

1

Why not do it in one go? Something like:

receiptsByPersonMap[_person].push(new Receipts(_person, _cost));

Incidentally, this may just be an artifact of the way you've simplified the question to post here, but since _person is the key of the mapping where you store your Receipts record, you can't access it unless you already know _person, so you should be able to remove _person from the struct. At that point the struct only contains one item, so you could get rid of it entirely and just store cost in a bytes32 => uint256[] mapping.

1
  • Thanks @EdmundEdgar. Your suggestion is perfect.. Yes, I had simplified the struct for this question.
    – userDSSR
    Oct 4 '17 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.