Mapping in Solidity

What is Mapping in Solidity?

As Solidity is heavily based on other languages like Javascript and Python. First, let's look at what Mapping means in Javascript.

In Javascript, the Map object got included in the language with the introduction of ES6. Map in Javascript is a set of elements in which each element is saved as a Key, value pair. Map objects can keep each object and primitive values as both key or value. while we iterate over the map object it returns the key, value pair within the identical order as inserted.

An example of Map in Javascript

var mapOne = new Map ([[5, 6], [7, 8], [9, 10]]); 
console.log("mapOne: ");
console.log(mapOne);

/* output
 0: {5 => 6};
 1: {7 => 8};
 2: {9 => 10};
*/

As you can see Map returns the object and key pair with a symbol like this '=>'. The objects here are 5, 7, 9 and the keys are 6, 8, 10.

Now let's take a look at how Mapping works in Solidity.

The general use case of Mapping in Solidity is to match the Wallet Address of an Ethereum user with the amount they have in their wallet. Mapping in Solidity acts like how a hash table or dictionary would act in other languages like Javascript and Python. Like we saw in the example above Solidity also stores the data in key => value pairs and returns them in the same key => value pair as well. Let's take a look at the syntax of how Mapping works in Solidity.

pragma solidity ^0.4.18; 
contract mapping_in_solidity {
struct students{
string name;
string subject; 
uint8 marks
}
// creating the mapping
mapping (address => student) result;
address [] student_result

// creating the function to add values to the mapping
function adding_marks() public {
var student = result[0xDEE7796E89C82C36BAdd1375076f39D69FafE252];
student.name = "Aditya";
student.subject = "Maths";
student.marks = 91;
student_result.push(0xDEE7796E89C82C36BAdd1375076f39D69FafE252) - 1;
}
// function to retrieve values from the mapping
function get_values() view public returns (address[]) {
return student_result
}
 }
/*
// OUTPUT
name: "Aditya" string
subject: "Maths" string
marks: 91 uint8
*/

Let's understand what is happening in the code.

As the standard rule of writing code in Solidity, one of the foremost steps is to specify the version number of the Solidity being used in our case it is "^0.4.18"

One the next line we give the name to our Solidity Smart Contract, i.e mapping_in_solidity. Next, we create a struct in which we specify the values which we want to map over and see the output after mapping. The struct contains a string for the name of the student, a string for the subject and uint8 for the marks (uint8 returns an integer value). Next, we call the actual mapping function in Solidity which returns the result. On the next line, we initialize an empty array to push values in before it gets mapped. Next, we create a function to set the values of the name, subject and marks.

/* student.name = "Aditya";
student.subject = "Maths";
student.marks = 91;
*/

Here we are setting the name, subject and the marks of the student to get mapped over. At last, we create a function to retrieve the values from the mapping function which returns the student_result which we had initialized at the start.

If you have any more questions you can find me on my Twitter and LinkedIn