Preface
Mappings store key-value pairs in Solidity, which similar to hash tables or dictionaries.
- definition & initialization
mapping(address => uint) public balances
Key types cannot be mappings or arrays.
Values can be any type, including nested mappings.
- writing and reading operation
writing/updating: [mappingName][key] = value;
reading: [mappingName][key]
Notes: reading a non-existent key returns the default value.
- limitation
mappings cannot be iterated
Auxiliary arrays can track keys
- nested mappings
mapping(address => mapping(uint => bool)) public permissions;
Nested mappings require multiple keys for access:
permissions[msg.sender][1]
Top comments (0)