Skip to content
the poor coder

for on mapping in solidity

Poor Man
Published
Reading time
3 min read
On this page

Introduction to Mapping in Solidity#

In Solidity, mapping is a reference type that is used to create key-value pairs. It is similar to a hash table or dictionary in other programming languages. Mappings are used to store and retrieve data efficiently by using keys to access values.

Mapping in Solidity is a highly useful data structure that can be used for a wide variety of purposes, such as storing user balances, keeping track of ownership, and tracking contract states.

Creating a Mapping#

To create a mapping in Solidity, you need to define the type of the key and the type of the value that will be stored in the mapping. You can use any valid data type as the key or the value in a mapping.

SOLIDITY

// Define a mapping that maps addresses to integers
mapping(address => uint) public balances;

Accessing Values in a Mapping#

To access a value stored in a mapping, you need to provide the key used to store the value in the mapping. If the key exists in the mapping, the value associated with the key will be returned. If the key does not exist in the mapping, the default value for the value's data type will be returned.

SOLIDITY

// Get the balance for a specific address
uint balance = balances[msg.sender];

Updating Values in a Mapping#

To update a value stored in a mapping, you can simply assign a new value to the key in the mapping. If the key does not exist in the mapping, a new key-value pair will be created with the assigned value.

SOLIDITY

// Update the balance for a specific address
balances[msg.sender] = 100;

Deleting Values in a Mapping#

To delete a value stored in a mapping, you can use the delete keyword followed by the name of the mapping and the key of the value to be deleted. If the key does not exist in the mapping, no action will be taken.

SOLIDITY

// Delete the balance for a specific address
delete balances[msg.sender];

Iterating over a Mapping#

Mapping in Solidity does not support iterating over all the key-value pairs. However, you can create an array or a list of keys and use it to iterate over the mapping.

SOLIDITY

// Create an array of addresses
address[] public accounts;

// Add an address to the array
function addAddress(address _address) public {
    accounts.push(_address);
}

// Get the balances for all addresses in the array
function getBalances() public view returns (uint[] memory) {
    uint[] memory result = new uint[](accounts.length);
    for (uint i = 0; i < accounts.length; i++) {
        result[i] = balances[accounts[i]];
    }
    return result;
}

In this example, an array of addresses is created and populated with some addresses. The getBalances() function is used to iterate over all the addresses in the array and retrieve their corresponding balances from the mapping.

Conclusion#

Mapping is a powerful data structure in Solidity that can help you store and retrieve data efficiently. It is easy to create, access, update, and delete values in a mapping. However, iterating over all the key-value pairs in a mapping can be challenging, so you may need to use other data structures or techniques to achieve your goal.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC