替换 Solidity Struct 中动态数组的值

Replacing the value of dynamic array inside a Solidity Struct

我在 UserListing 中有一个动态数组,在访问动态数组 Listing[] 以在 solidity 编程中将状态更改为 Canceled 时遇到问题。 目前的推送功能只是在列表中追加一个数组,请问是否可以删除之前的列表并替换为新列表/更改列表中动态数组中的值[].如有帮助,万分感谢:)

struct UserListing {
    address ownerAddress;
    Listing[] list;
}

struct Listing {
    uint listingId;
    ListingStatus status;
    address seller;
    address token;
    uint tokenId;
    uint price;
}

uint private _listingId = 0;

mapping(uint => Listing) private _listings;
mapping(address => UserListing) public userListings;

function cancel(uint listingId) public {
    Listing storage listing = _listings[listingId];

    require(msg.sender == listing.seller, "Only seller can cancel listing");
    require(listing.status == ListingStatus.Active, "Listing is not active");

    
    listing.status = ListingStatus.Cancelled;

    userListings[msg.sender].list.push(
        Listing({
            listingId : _listingId,
            status: ListingStatus.Cancelled,
            seller : msg.sender,
            token: listing.token,
            tokenId : listing.tokenId,
            price : listing.price
        })
    );

    IntERC721(listing.token).transferFrom(address(this), msg.sender, listing.tokenId);

    emit Cancel(listingId, listing.seller);
}

据我了解,在 cancel 中,您想在给定 listingId 的用户列表中找到项目(列表)并更改其状态。为此,您不需要删除任何内容。您只需遍历用户列表。找到 Listing 并更改其状态。

 // i will modify userItems
 // since state variables are storage by default, modifying this will modify the state of the contract
 Listing[] storage userItems = userListings[msg.sender]

现在 运行 一个 for 循环:

for(uint256 i=0; i<userItems.length; i++){

   if userItems[i].listingId==listingId{
      userItems[i].status=ListingStatus.Cancelled,
}