0

I have a mapping defined in my ethereum contract as follows

mapping (string => string) client;

I have a function that is trying to add clients to this mapping but before i add i wanna check if item already exists. Is there a way i can get index of an item in that either by a out of the box method which is like "get me the index of item in this mapping if value is this" or maybe iterate through the mapping inside the contract

function AddClient(string clientName) {

}
Asif
  • 393
  • 9
  • 17
  • I accomplished this by keeping a parallel array of the key names. Then you can use a for loop to get the index for a key and a simple return to get the mapping element at the index, e.g., myMappingElements[myKeyElements[i]]. – Chris B. Behrens Apr 01 '22 at 20:07

1 Answers1

2

There are a few ways to accomplish it but you have to manage things at a lower level because there is no out-of-the-box way to interrogate the mapping for that information. Have a look at this piece that describes some basic storage patterns. https://ethereum.stackexchange.com/questions/13167/are-there-well-solved-and-simple-storage-patterns-for-solidity

Hope it helps.

Rob Hitchens
  • 1,049
  • 10
  • 14