1

In solidity I wonder if I can define an array that can store array of strut inside another array I have gave an example bellow.

[ 
  [{id: 1, val: "val1"},{id: 2, val: "val2"},{id: 3, val: "val3"}],  
  [{id: 4, val: "val4"},{id: 5, val: "val5"},{id: 6, val: "val6"}],
]

Earlier I had a simple array using which I was just pushing single struct, but in that approach I have to pay gas fee for each array push

So I though if I can make a single list of multiple struct and call push method for that one array then I might be able to save a lot of gas.

I have tried achieving this using the snippet bellow but it doesn't work

struct Emp {
uint256 id;
string val;
}

Emp[][] public emprec;

function addEmpArr(Emp[][] memory arr) external {
emprec.push(arr);
}

1 Answer 1

0

Maybe you can do something with mappings? mapping( uint256 => mapping( uint256 => Emp ) ) empRec or mapping( uint256 => Emp[]) empRec

2
  • Thanks for responding. Following your idea i tried to implement the same with mappings. But am not able to figure out how to work with this type of structure. Could you please give me a working example ?
    – Klr v
    Commented Dec 29, 2022 at 6:16
  • and while getting this data what will be return type ? like function getEmp() external view returns (empRec memory) { return emprec; }
    – Klr v
    Commented Dec 29, 2022 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.