How to access Struct field dynamically

by alia   Last Updated September 11, 2019 09:28 AM - source

I have a struct

    struct lineItem {
        string idNo;
        string date;
        string code;
        string docCode;
        string vendorNumber;
        string invoiceDate;
        string typeOfTransaction;

I have a mapping

    mapping(string => lineItem[]) lineItems;//here string will be the user who adds data to this

So i have to filter the mapping according to the username given and after that i have to filter those data having idNo same as the input

      function getByInternalDocNo(string memory creator,string memory docNo,string memory typeOfFilter) public view returns(lineItem[] memory,vatDetails[] memory){
        uint count = 0;
        // bool[] memory res = new bool[](lineItems[creator].length);
        for (uint index = 0;index<lineItems[creator].length;index++){
            if(keccak256(bytes(lineItems[creator][index].idNo))==keccak256(bytes(docNo))) {
        lineItem[] memory res1 = new lineItem[](count+1);
        vatDetails[] memory res2 = new vatDetails[](count+1);
        count = 0;
        for (uint index = 0;index<lineItems[creator].length;index++){
            if(keccak256(bytes(lineItems[creator][index].idNo))==keccak256(bytes(docNo))) {
                res1[count] = lineItems[creator][index];
                res2[count] = vatDetail[creator][index];
        return (res1,res2);

Now i want to make this function a bit more generic for all type of filters i.e. to check whether coCode is equal to given value or not.So,i wanted to do something like this

lineItems[creator][index][typeOfFilter]// will give error

and use this to compare it with the input value.So how do i do this?

Tags : solidity

Related Questions

How to map an address with a string array in solidity?

Updated October 09, 2018 12:28 PM

How to handle dynamic size string array in solidity?

Updated November 28, 2018 04:28 AM

What is the status of forbidden call in constructor?

Updated December 12, 2018 08:28 AM

How can I pass struct from one contract to another?

Updated January 10, 2019 23:28 PM

How to transfer Ether to ERC20 token contract?

Updated July 23, 2018 00:28 AM