I want a way to store an array of structs. And then I want the ability to filter through this array and pick out a subset of items where a certain struct parameter named foo == bar for example.

Is this possible in solidity? Are there any limitations on how large my original my array is and the number of returned items?

Lastly, because this would only read from the ledger, it should not cost any gas to execute correct?

Thank you.

2 Answers 2


As I understand its possible with followings,

Are there any limitations on how large my original my array

The limitation to contracts' storage is the limitation for this as well - i.e. 2^261 bytes. You may refer this question for contract storage limitation.

number of returned items?

There are two limitations; what solidity has allowed to return from a function which has a good read here and what your client machine used to connect to the ethereum network can handle in it's memory.

because this would only read from the ledger, it should not cost any gas to execute correct

This depends on your requirement. If what you want is just to read without a transaction involved (to display locally or so) it won't cost gas. But although the function is constant, if you need it to be used within a transaction (let's say you call non-constant function and within that function you use this constan function to verify something ) this may still cost gas. A good read regarding this can be found in this question.

code would look like something similar to this, depending on your requirement it may change

MyStruct[] myStructArray;

function checkArray() constant returns(bool[]){ // change on what you need to return

    bool[] checker; // change on what you need to return

    for(uint i = 0; i<myStructArray.length; i++){
        if(myStructArray[i].foo == bar){
            checker[i]=true; //or whatever you want to do if it matches
    return checker;
  • 1
    Thank you for your answer. Could you give me an example code snippet to do the actual 'filter' procedure?
    – Raven
    Oct 11, 2017 at 0:27
  • 1
    ah I see, thank you. I've accepted your answer as it hits all my points and explains them well. Thank you friend =)
    – Raven
    Oct 11, 2017 at 11:25

All of this is possible, but there are limitations of course. How large your array can be will depend on what your struct holds. Can't say how many elements specifically you will be able to hold, but obviously a struct with 10 strings will consume far more gas to manipulate than a struct with say 2 uint8.

Also, depending on your use cases, you might want to have the contract return the entire array and do the filtering client-side. It really depends on what you are trying to achieve and how big / difficult to filter your data is.

When reading data you are still limited to gas requirements, but the user does not pay for it. So, even if you are not paying for the gas, you won't be able to return an array with millions of structs as the function call will run out of gas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.