Solidity Mappings – Access All Values in Mappings of Solidity Smart Contract

contract-designcontract-developmentsolidity

This question addresses how a value stored in a contract variable from the outside.

Is there any similar way to access a value of tuples stored in mapping(with and without knowing a key of mapping).

Consider the following smart contract.

pragma solidity ^0.4.24;
contract Student{
    struct stu{
        string name;
        uint age;
        bool tookTest;
    }
    mapping(uint => stu) studentNames;
    function addStudent (uint ID, string _name, uint _age) {
        studentNames[ID] = stu(_name, _age, false);
    }
    function updateStudent (uint ID) {
        studentNames[ID].tookTest = true;
    }
}

I want to all the tuples stored in studentNames and I know only contract address.

Is that possible?

Best Answer

You can do it if you add one array to store the mapping key; then you can return the key array to client, and client can iterate keys to get the student information for each key.

Related Topic