gpt4 book ai didi

How to clear and reset Mapping of Array in solidity(如何清除和重置实心数组的映射)

转载 作者:bug小助手 更新时间:2023-10-28 10:40:10 25 4
gpt4 key购买 nike



Below is how I do the record into the mapping array. How can I create a clear|removeall function that clears or reset all the records back to default which is empty?

下面是我如何将记录放入映射数组中。我怎样才能建立一个清晰的|removeall函数,清除或重置所有记录回到默认值,哪个为空?


    address payable[] public players;
mapping(address => uint256[]) playerTicket;


function playersRecord() public view returns(uint256[] memory){
return playerTicket[msg.sender];
}

I managed with the below function to clear one by one but not sure how to clear all

我用下面的功能逐个清除了,但不确定如何清除所有


function remove(address _addr) public {
// Reset the value to the default value.
delete playerTicket[_addr];
}

更多回答
优秀答案推荐

You cannot clear all mapping values without specificy the key.
Thus, Solidity doesn't know the keys of the mapping. Since the keys are arbitrary and not stored along, the only way to delete values is to know the key for each stored value.

Your remove() function is correct to clear the values of a specific mapping key.

如果不指定键,则无法清除所有映射值。因此,实心度不知道映射的关键。由于密钥是任意的并且不存储在一起,因此删除值的唯一方法是知道每个存储值的密钥。您的Remove()函数清除特定映射键的值是正确的。


More information here.

更多信息请点击此处。



use delete keyword in latest version.
working example - refer this repo

在最新版本中使用删除关键字。工作示例-参考此回购


delete players;


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com