2

I use solidity with web3.js and know that we actually cannot return the whole struct array, but I would like to know if it is possible to return only a type in the struct as an array.

Lets say i have the following struct:

struct aStruct {
    bytes8 a; 
    bytes8 b;
}

aStruct[10000] aStr;

Can I now make a function, where I return aStr.a, the whole "a" as an array?

HansPeterLoft
  • 161
  • 1
  • 6

3 Answers3

1

I'm going to go out on a limb here and say you probably don't want to do that, even if you could, which you can't, at least for now.

The blockchain is not like other platforms. Some of the changes are hard to accept because they're opposite of what experience teaches.

Iteration over a set is almost always a client-side concern. It's easy to see how a client with the whole data set can figure that out on its own. You shouldn't burden the contract with a concern the clients can address without assistance.

This might help explain where I'm coming from. https://medium.com/p/2303010c8b09/

jordiburgos
  • 103
  • 5
Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
0

You can try something like this:

function yourFunc(uint index) public returns(bytes8)
{
 return aStr[index].a;
}
Kashish Khullar
  • 1,563
  • 3
  • 14
  • 26
0

If you have a fixed length of an array, you can easily loop over and append all desired values in a string or a series of strings and then return it to your client and properly parse the resulting data.

to find more, try referring to this question : How to convert a bytes32 to string

Kaki Master Of Time
  • 3,060
  • 4
  • 19
  • 44