0

How am I supposed to reset an array with previous values to just an empty array? Can't figure it out for the life of me. Using solidity version 4.26, tried...

tokenContract[tokenAddress].someArray = new uint256[];
tokenContract[tokenAddress].someArray = [];
tokenContract[tokenAddress].someArray = uint256[];

It's a mapping with an address attached and then linked to an array but I doubt that has any influence on it.

resolved My issue wasn't the ability to delete the array but that I was using a loop to do so, and since my loop integer was (and must be) a uint (unsigned integer that cannot become negative), on the last iteration it would break. Hence, if you're running through an array and get to the last element, please insert the following...

if (x == 0) {
return;
}

Hopefully, it will save you the whole day. Thank you for the answers.

1

You can set length = 0 or create an empty array and assign in back to your mapping.

pragma solidity 0.5.11;

contract TestArray {
    mapping(uint => uint[]) users;

    function addUser(uint _id, uint[] memory _users) public {
        users[_id] = _users;   
    }

    function getUsers(uint _id) public view returns(uint[] memory){
        return users[_id];
    }

    function emptyUsers(uint _id) public {
        uint[] memory empty;
        users[_id] = empty;
        // or 
        //users[_id].length = 0;
    }
}
| improve this answer | |
1

Try tokenContract[tokenAddress].someArray.length = 0;.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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