Hello readers, in this article, I will be explaining explicitly what we called Iterable mapping using code.
First step is to declare your pragma solidity and the contract name.
pragma solidity ^0.8.0;
contract Michael{
}
Declare mapping balances which keeps track of the balance of our users.
Note: mapping takes the key => value pair. So the key here is address while the value is uint. Let me calm down to explain.
The address is the address of our users while the uint is the data type for the amount. Public is the visibility identifier while the balances is the balance of the address.
mapping(address => uint) public balances;
Let create another mapping called inserted.
mapping(address => bool) public inserted;
The above mapping keeps track of the address and the bool data type. That will enable us to know if the address has been inserted or not.
When an address is inserted, the bool changes to true. You will see how the bool will change to true later on.
Next is to create an array to save the address and amount.
address[] public keys;
See the full code that comprises of the mappings and array.
pragma solidity ^0.8.0;
contract Michael{
mapping(address => uint) public balances;
mapping(address => bool) public inserted;
address[] public keys;
}
Let us create a function where we can set data into the mapping.
function set(address _x, uint amount)public {
}
Save the amount in balances mapping.
balances[_x] = amount;
The above code simply means that you are updating balances mapping and you are inserting inside the mapping the address and amount;
Don't be tired, let continue.
Next is to check if the address has not been inserted before.
if(!inserted[_x]){
inserted[_x] = true;
}
If the address has not been inserted before, then update the address status to true. Then push the address into the array keys by using array.push method.
if(!inserted[_x]){
inserted[_x] = true;
keys.push(_x);
}
That being said, let create a function that can get the size of the array.
function getSize() public view returns(uint){
return keys.length;
}
To get the size of an array, the formula is array.length. As you can see in the above code, I used the name of the array dot length.
Next is to create a function that gets the first element in the array.
function firstIndex() public view returns(uint){
return balances[keys[0]];
}
The above code captures the first element in the keys array starting from balances mapping, then moving to keys and indexing the one at position zero which happens to be the first item in the array.
Next step is to get the last item in the array.
function LastItem() public view returns(uint){
return balances[keys[keys.length -1]];
}
The method we used to get the last item of the array is by moving from balances mapping to keys array then we specified the array.length minus 1 (This is used to capture last item in an array).
The last thing for us to do is to create a function that can capture any index in the keys array.
function AnyIndex(uint _x) public view returns(uint){
return balances[keys[_x]];
}
We will move from balances to keys array then we input the index from frontend.
See the full code below.
pragma solidity ^0.8.0;
contract Michael{
mapping(address => uint) public balances;
mapping(address => bool) public inserted;
address[] public keys;
function set(address _x, uint amount)public {
balances[_x] = amount;
if(!inserted[_x]){
inserted[_x] = true;
keys.push(_x);
}
}
function getSize() public view returns(uint){
return keys.length;
}
function firstIndex() public view returns(uint){
return balances[keys[0]];
}
function LastItem() public view returns(uint){
return balances[keys[keys.length -1]];
}
function AnyIndex(uint _x) public view returns(uint){
return balances[keys[_x]];
}
}
Congratulations, we are done with iterable mapping.