How can I map multiple struct instances to one user without overriding the previous instance? I want to retrieve both instances through one mapping query. Should I use a multi-dimensional array here or a nested mapping?

As an example. If I have the below two structs. The problem is that the cookie mapping will only retrieve the latest cookie hidden by the cookieContributor but I want to retrieve all the cookies contributed by this particular address? How should my mapping look to achieve this?

contract HiddenCookies {

    struct Cookie {
        string cookieID
        string name,
        string color,
        string flavor,
        uint amount

    struct CookieJar {
        address[] cookieContributors; 
        mapping(address => Cookie[]) cookies;
        Cookie[] cookieJarContent;
        uint cookiesHiddenForLater;
  • 1
    Sounds like mapping(address => YourStruct[]) should do the job, though it's hard to say for sure, when relying on your extremely too-broad description! Jul 11, 2019 at 15:27
  • I edited the question to hopefully shed more light on my problem @goodvibration
    – NowsyMe
    Jul 11, 2019 at 17:36

1 Answer 1


goodvibration's answer is correct, but just to expand a bit:

When you use mapping(address => Cookie[]) cookie; what you are getting is a multidimensional array, in a way. There's a "list" of addresses (the mapping) and each address has a list of Cookies (the array Cookies[]).

Then, when a user sends a Cookie to the CookieJar, instead of setting the Cookie with

cookie[msg.sender] = myCookie;

You'd do something like


As a suggestion, I'd change the name of the mapping from cookie to cookies (plural) because I believe it's more readable that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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