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?

3 Answers 3

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/

0

You can try something like this:

function yourFunc(uint index) public returns(bytes8)
{
 return aStr[index].a;
}
4
  • Yes, but this returns only the element at the index, I would like to return the array of all the indices at the same time. Commented Jan 9, 2018 at 11:55
  • why would you want that? Commented Jan 9, 2018 at 16:46
  • I want to return data, that is manipulated by everyone but commonly shared. Commented Jan 10, 2018 at 5:46
  • You cannot return variable sized array in solidity, not yet, but yes you can return a fixed size array Commented Jan 10, 2018 at 9:47
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

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.