Is there a way for me to get the total number of elements in a mapping in Solidity?
Asked
Active
Viewed 3,427 times
4
-
Not unless you count it by yourself (increment after the creation of a new entry, decrement after the deletion of an existing entry). – sea212 Apr 05 '20 at 22:43
-
Does this help? Are there well-solved and simple storage patterns for Solidity? – Paul Razvan Berg Nov 06 '21 at 13:28
2 Answers
5
You can't. There is no way to know how many entries there are in a mapping.
If you need to keep tract of the mapping's elements you can use a separate array which holds extra information about the mapping elements
Lauri Peltonen
- 29,391
- 3
- 20
- 57
0
You cannot do it as @Lauri mentioned. You can check how to do it manually in my answer here.
Abdul Razak Zakieh
- 141
- 5