Skip to content
the poor coder

reinitialise or reset all values in mapping in solidity

Poor Man
Published
Reading time
2 min read
On this page

Reinitialise or Reset All Values in Mapping in Solidity#

Mapping is a data structure in Solidity that is used to store key-value pairs. Sometimes, it becomes necessary to reset or reinitialize all the values in a mapping. There are different ways to do this, let's explore them.

Method 1: Deleting Each Key-Value Pair#

The first method to reset all the values in a mapping is to delete each key-value pair. This method is straightforward and easy to understand. We can use the delete keyword to delete each key-value pair:

Text

mapping (uint => uint) myMapping;

// Adding values to the mapping
myMapping[1] = 10;
myMapping[2] = 20;
myMapping[3] = 30;

// Deleting each key-value pair
delete myMapping[1];
delete myMapping[2];
delete myMapping[3];

This method works well if the mapping has a few key-value pairs. But if the mapping has many key-value pairs, it may not be practical to delete each key-value pair manually.

Method 2: For Loop to Delete Each Key-Value Pair#

The second method is to use a for loop to delete each key-value pair. This method is useful when we have many key-value pairs in the mapping. Here’s an example:

Text

mapping (uint => uint) myMapping;

// Adding values to the mapping
myMapping[1] = 10;
myMapping[2] = 20;
myMapping[3] = 30;

// Resetting all values in the mapping
for(uint i=1; i<=3; i++) {
    delete myMapping[i];
}

In this method, we use a for loop to iterate over each key-value pair and delete them using the delete keyword.

Method 3: Using a Struct#

The third method is to use a struct to store the values of the mapping. We can then reset the struct to reset all the values in the mapping. Here’s an example:

Text

struct MyStruct {
    uint value1;
    uint value2;
    uint value3;
}

mapping (uint => MyStruct) myMapping;

// Adding values to the mapping
myMapping[1] = MyStruct(10, 20, 30);

// Resetting all values in the mapping
myMapping[1] = MyStruct(0, 0, 0);

In this method, we create a struct that stores the values of the mapping. We add values to the mapping by creating a new instance of the struct and assigning it to the key. To reset all the values in the mapping, we create a new instance of the struct with all values set to zero and assign it to the same key.

Conclusion#

These are three methods to reset or reinitialize all the values in a mapping in Solidity. The method you choose depends on the number of key-value pairs in the mapping and your programming needs.

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