0

I want to create a mapping with an array of values. For example, I want to create a mapping of address that will hold the array of tokenIds that address holds.

0x0000XXXXX[0] = 989

0x0000XXXXX[1] = 121

Below is what I am trying in code:

struct Token {
    uint token;
}
mapping(address => Token[]) public addressMintedTokens;

function setTokens(_receiver, tokenId) internal {
addressMintedTokens[_receiver].push(tokenId);
}

Unfortunately, this is not working and giving an error Member "push" not found or not visible after argument-dependent lookup in struct

1 Answer 1

0

To insert a key-value pair in your mapping, you can either use this syntax:

uint tokenId = 1729;
addressMintedTokens[_receiver] = Token({ token: tokenId });

Or this syntax:

uint tokenId = 1729;
addressMintedTokens[msg.sender].push(Token(tokenId));
2
  • This worked for me. uint tokenId = 1729; addressMintedTokens[msg.sender].push(Token(tokenId));
    – Ankesh
    Commented Jun 10, 2022 at 7:36
  • @AnkeshSrivastava I have updated my answer to account for the alternative solution Commented Jun 10, 2022 at 21:25

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.