1

Here is an uint array defined in smart contract:

uint256[] public bidderItemID;

And bidderItemID is assigned value in contract:

bidderItemID = [1,2,3];

How can I empty bidderItemID after it is assigned a value? That is, bidderItemID.length is 0 and there is no element in bidderItemID. I tried bidderItemID=[] and it throws an error.

1
  • Note that, if you have a large array, it's probably not worth it to reset the array, because of the high gas costs.
    – hrkrshnn
    Commented Feb 18, 2021 at 11:32

1 Answer 1

1

You can delete an array to achieve your goal. See the following code as an example.

pragma solidity 0.6.12;


contract Test {
    uint256[] public bidderItemID;
    
    function setBidderItemID() public {
        bidderItemID = [1,2,3];
    }
    
    function deleteBidderItemID() public {
        delete bidderItemID;
    }
}

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.