[Ethereum] Getting key from Solidity mapping by value

contract-designcontract-developmentsolidityweb3js

I want to store a key-value pair in a Smart Contract.
For example:

mapping(uint => address) public myMapping;

And I would like to have public access to both:

  1. value by key
  2. key by value

I can get value by key in web3js by this way:

contract.myMapping.call(key, function(err, val) {
   ...
});

Is it possible to get key by value from mapping using web3js, or access in both directions can be done only with creating of 2 mappings?

Best Answer

There is no built-way in Solidity or web3.js to get a key from a value.

Using 2 mappings is typical for a bidirectional map.

Related Topic