Skip to content

Instantly share code, notes, and snippets.

@samanshahmohamadi
Created January 15, 2019 09:40
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save samanshahmohamadi/d0d43034e45ccadfd48ce97ed0bd1aba to your computer and use it in GitHub Desktop.
Save samanshahmohamadi/d0d43034e45ccadfd48ce97ed0bd1aba to your computer and use it in GitHub Desktop.
How to return whole mapping in solidity
pragma solidity ^0.4.25;
contract MappingTest {
mapping(uint=>address) public addresses;
uint addressRegistryCount;
function set(address userAddress) public{
addresses[addressRegistryCount] = userAddress;
addressRegistryCount++;
}
function getAll() public view returns (address[] memory){
address[] memory ret = new address[](addressRegistryCount);
for (uint i = 0; i < addressRegistryCount; i++) {
ret[i] = addresses[i];
}
return ret;
}
}
@somoza
Copy link

somoza commented Jul 9, 2021

I've noted that the response of this is similar to this (I'm using string array instead address array):

{ "0": "string[]: Test1,Test2" }

How catcheable is it this for Dapp? What does finally receive JS? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment