i'm trying to assign array of items to one user and i want to put address to mapping function and get all items like that:

mapping(address => Item[]) public ownerToItems;

but there is problem with assignment, first time accessing this array will be empty. How example like this should be build?

1 Answer 1


Try this:

function add(address owner, uint arg1, uint arg2, uint arg3) external {
    ownerToItems[owner].push(Item(arg1, arg2, arg3));
  • This is good, can you also tell me how to returns this items to read all in front-end? In solidity we can't returns array so what it should looks like?
    – Pawelo
    Aug 28, 2018 at 15:29
  • Just to correct that bit of (common) misinformation, you can return arrays from your smart contract. You're probably thinking about the ABI encoder's limitation around returning nested arrays/structs.
    – user19510
    Aug 28, 2018 at 16:12
  • You can easily return the items one at a time. (In fact, because of your public declaration, clients should already be able to call ownerToItems(address addr, uint256 index). You probably want to add a view function that returns ownerToItems[addr].length to make enumeration easier on the client.
    – user19510
    Aug 28, 2018 at 16:13

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.