简体   繁体   English

在 Solidity 中检索嵌套映射

[英]Retrieve nested mapping in Solidity

Pls, help to figure out how to retrieve the nested mapping in the Solidity.请帮助弄清楚如何在 Solidity 中检索嵌套映射。

string tokenURI;
uint256 tokenId;

There is Solidity mapping:有 Solidity 映射:

mapping(uint256 => mapping(address => string);

How to get all tokenURI based on address?如何根据地址获取所有 tokenURI? I need to print them out using Python.我需要使用 Python 将它们打印出来。

I created loop but it doesn't work:我创建了循环,但它不起作用:

uint256[] public tokens; // numbers 0,1,2 etc 
address[] public clients; // addresses  
string[] public uris;  
function getNftUri() public {
         for (uint256 i = 0; i < tokens.length; i++){
             uint256 t = tokens[i];
             for (uint256 j = 0; j < clients.length; j++){
                 address client = clients[j];
                 if (client == msg.sender){
                     uris.push(nftCollection2[t][client]);
                 }
             }
         }
         //return uris; 
}

Soldity mappings are not iterable, so you'll need to duplicate the values in an array to be able to get all values by a key. Soldity 映射不可迭代,因此您需要复制数组中的值才能通过键获取所有值。

mapping(address => mapping(uint256 => string)) tokenURIs;

// this way you can query `tokenURIsByAddress[owner]` to get all token URIs by an address
// `push()` to this array to add items
// as well as remove items from the array
mapping(address => string[]) tokenURIsByAddress;

You can also make use of some existing library for iterable mappings that does exactly the same thing as described in my code comment above.您还可以利用一些现有的库进行可迭代映射,它们的作用与我上面的代码注释中描述的完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM