Search code examples
mappingethereumcontract

How to find index of item in contract mapping


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) {

}

Solution

  • 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.