0

So, for example, if I have the following mapping in my Solidity code:

mapping (uint => myStruct) myMapping;

If let's say I have an object of this mapping. For example, myMapping[1] is mapped to a struct with certain values. But now I'd like myMapping[1] to be mapped to a NULL value or an empty myStruct.

How can I achieve this in my Solidity code?

2 Answers 2

1

You can try the code bellow. Just update the key with a blank structure. It will make the struct associated with the key to return to its default values.

pragma solidity ^0.8.0;

contract foo {

struct Test {
    uint myUint;
    string myString;
}

mapping(uint => Test) public myMapping;

function setMapping(uint key, uint _myUint, string memory _myString) public {
    Test memory test;
    test.myUint = _myUint;
    test.myString = _myString;
    myMapping[key] = test;
}

function nullifyStruct(uint key) public {
    Test memory test;
    myMapping[key] = test;
}


}
1

You can use the keyword delete

delete myMapping[key];

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.