Solidity – How to Check if Key Exists in Mapping of Mapping

solidity

I have below data structure used in my solidity code.

// map(address => map(questionId => voted))
mapping(address => mapping(uint256 => bool)) public mapUserVotes;

My requirement is that given an address, I need to check if it 'exists' in this mapping (ie has at least one quesitionId mapped to it).

I understand that all keys do exists in mapping, and maps to 0 by default.

Wanted to understand is there a way to achieve this? Thanks

Best Answer

Mappings initialize with default values, usually a form of 0 (e.g. uint is 0, address will be address(0) etc.) but there isn't a default "null" for mappings as values. You may need a way to set the status of a user address being initialized. For example:

  1. map address to a struct that includes the value mapping and an extra boolean or
  2. creating a separate mapping (address => bool) that it set to true as soon as you add an entry to mapUserVotes for that address.
Related Topic