Exploring Nested Mappings in Solidity

Kristaps Grinbergs
3 min readFeb 28, 2024

Nested mappings in Solidity offer developers powerful tools to efficiently organize and manipulate complex data structures. This blog post explores how to use nested mappings and their practical applications and provides transformative examples in smart contract development.

Photo by Alain Pham on Unsplash

Nested Mapping in Solidity

In Solidity, nested mapping involves using one mapping as the value type within another mapping. This technique enables developers to create multidimensional data structures within Ethereum smart contracts. Nested mappings facilitate hierarchical relationships between data elements.

In this post, we’ll delve into an illustrative example showcasing how to organize employee records based on their respective departments efficiently.

First, we need to define a nested mapping. We can achieve that like a regular mapping, but we use another mapping instead of type.

mapping(string => mapping(address => Employee)) private employees;

In this example, we will use a Solidity struct type that holds employee data like name and salary.

struct Employee {
string name;
uint256 salary;
}

Storing Values in a Nested Mapping

--

--

Kristaps Grinbergs
Kristaps Grinbergs

Written by Kristaps Grinbergs

Blockchain, mobile and fullstack developer. Startup founder. Conference speaker. Mentor. Passionate about building products, sustainability and Web 3.0.

No responses yet