-1

I want to store some address and their ids in blockchain using mapping and i want to know how to get the stored addresses when searching with their respective IDs.

0

When using mapping one of the problems is that there is no way to enumerate over the values. Whatever you store in the mapping with a key can only be accessed with that key and no other way.

So, in order to store both the mapping data and data about which entries have mapping data you have to use two different structures. One mapping for the actual mapping and one array to store information about which keys have a mapping.

If I understood your question correctly you want some way to search through the added ids.

So here's a very simple example contract. If I misunderstood and you don't need any searching possibilities you can just remove the ids part. With the below example you can add some functionality to search through the ids array:

pragma solidity ^0.5.5;

contract Mapping {

    mapping(uint => address) map;
    uint[] ids;

    function mapAddr(address addr, uint id) public {
        map[id] = addr;
        ids.push(id);
    }

    function getAddr(uint id) public returns (address) {
        return map[id];
    }
}

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.