2

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

1 Answer 1

1

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.
1
  • Yeah, actually I wanted to do it without using any extra variable/mapping. Seems there is no other way. Anyways, I will mark this as correct answer. Thanks @razgraf
    – VikasGS
    Jan 17, 2022 at 17:45

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.