Skip to content
Home » Answers » How to return an array of structs in Solidity?

How to return an array of structs in Solidity?

question card saying how can I return an array of structs in Solidity

To return an array of structs in Solidity, you can create a function that returns a dynamically sized array of the struct type. The function can then loop through the data and add each struct to the array. The array can be returned at the end of the function.

In order to return an array of structs in Solidity, you can use the following syntax:

function getStructArray() public view returns (Struct[] memory) {
  Struct[] memory result = new Struct[](10);
  // populate the array with structs
  return result;
}

In this example, Struct is the name of the struct you want to return and result is the name of the array that will hold the structs. The memory keyword is used to allocate the array in memory.

Also check out 👉🏼 Deep Dive into Object Oriented Programming (OOP)

You can also return a dynamic array of structs using the mapping data type:

mapping (uint => Struct) public structArray;

function getStructArray() public view returns (Struct[] memory) {
  Struct[] memory result = new Struct[](structArray.length);
  uint i = 0;
  for (uint id in structArray) {
    result[i] = structArray[id];
    i++;
  }
  return result;
}

In this example, structArray is a mapping that maps a uint (e.g. an ID) to a Struct. The getStructArray() function iterates over the keys in the mapping, populates the result array with the structs from the mapping, and then returns the array.

Here is another example:

struct User {
  address addr;
  string name;
  uint age;
}

User[] public users;

function getUsers() public view returns (User[] memory) {
  User[] memory userArray = new User[](users.length);

  for (uint i = 0; i < users.length; i++) {
    userArray[i] = users[i];
  }

  return userArray;
}

In this example, the getUsers function creates a new dynamically sized array of User structs and loops through the users array, adding each struct to the new array. The array is then returned at the end of the function.

We also asked our teammate, Aswin, and this is how he said he’d approach it 👇🏼

Ways to return an array of structs in Solidity

1. Use a mapping to store the structs

Then loop through the mapping to retrieve each struct and add it to the array.

struct MyStruct {
  uint256 id;
  string name;
}

mapping (uint256 => MyStruct) public myStructs;

function getMyStructs() public view returns (MyStruct[] memory) {
  MyStruct[] memory result = new MyStruct[](myStructs.length);
  uint256 i = 0;
  for (uint256 id in myStructs) {
    result[i] = myStructs[id];
    i++;
  }
  return result;
}

2. Use a dynamic array to store the structs

Then loop through the array to retrieve each struct and add it to the result array.

struct MyStruct {
  uint256 id;
  string name;
}

MyStruct[] public myStructs;

function getMyStructs() public view returns (MyStruct[] memory) {
  MyStruct[] memory result = new MyStruct[](myStructs.length);
  for (uint256 i = 0; i < myStructs.length; i++) {
    result[i] = myStructs[i];
  }
  return result;
}

3. Use a struct that contains an array of structs

And return the struct.

struct MyStruct {
  uint256 id;
  string name;
}

struct MyStructs {
  MyStruct[] myStructs;
}

MyStructs public myStructs;

function getMyStructs() public view returns (MyStructs memory) {
  return myStructs;
}

Note that in all of these scenarios, the returned array will be a copy of the original array and modifying it will not affect the original array in the contract. To modify the original array, the contract would need to have a function to update the array and the user would need to call that function.

Hopefully this answer helps you understand how to return an array of structs in Solidity. If you have any suggestions, feedback or got something nice to say – leave a comment below and say hi 👋🏼

Leave a Reply

Your email address will not be published.