Save an array in mapping and get the value in Solidity

arraysmappingsolidity

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

Best Answer

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));