Mapping
Solidity 中的嵌套映射
此程式碼需要兩個輸入(_Id,_name)來獲取年齡我只需要從輸入 id 中獲取姓名和年齡
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; contract nestmap { mapping(uint256=>mapping(string=>uint256)) public User; function adduser(uint256 _Id,string memory _name, uint256 _age)public { User[_Id][_name]=_age; } } function user(uint256 _Id,string memory _name)public view returns(uint256) { return User[_Id][_name]; }
你可以試試這個:
pragma solidity 0.8.7; contract nestmap { struct Person{ uint256 age; string name; } mapping (uint256 => Person) public User; function adduser(uint256 _Id, uint256 _age, string memory _name) public { User[_Id].age = _age; User[_Id].name = _name; } function user(uint256 _Id) public view returns(Person memory) { return User[_Id]; } }