简体   繁体   中英

how i display return value eg: phone number : balance [9222111555 : 150] in solidity

    //Owner to Phone
    mapping(address => uint256[]) public phones;
    // Phone to Balance
    mapping(uint256 => uint256) public balance;

function register(uint256 phone, uint256 Balance) public {
        _mint(msg.sender, phone);
        phones[msg.sender].push(phone);
        balance[phone] = Balance;
    }
     
    function details(address owner) public view returns(uint256[] memory){
        return (phones[owner]);
    }
    

When i call the function details it gives me return only no. those i have mint, but i want

{

"9222111888": "150",

"9093164641": "550",

}

Not exact output, but if you want to return balances as well, you need to use struct. for example

struct Data {
    uint256 number;
    uint256 balance;
}

...

function details(address owner) public view returns (Data[] memory data) {
    uint256[] memory ownerPhones = phones[owner];
    uint256 numPhones = ownerPhones.length;
    data = new Data[](numPhones);

    uint256 number;

    for (uint256 i = 0; i < numPhones; i++) {
        number = ownerPhones[i];
        data[i].number = number;
        data[i].balance = balance[number];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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