4

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?

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.