1

i want to use push function in array inside mapping

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract mappingStruct {

mapping (uint=>string[]) mappingAndArray;
function addToMapping(uint myKey,string[] memory name)public{
    
}
}

2 Answers 2

2

This seems straightforward enough. Is this what you had in mind?

pragma solidity ^0.8.20;

contract MappingPush {

    mapping (uint256 => string[]) public myArrays;

    function pushTo(uint256 key, string calldata toAdd) public {
        myArrays[key].push(toAdd);
    }

}
1

You can try this (tested in Remix). This will assign a string array to each myKey:

pragma solidity ^0.8.7;
   contract mappingStruct {

   mapping (uint=>string[]) mappingAndArray;

   function addToMapping(uint myKey,string[] memory name)public{
       mappingAndArray[myKey] = name;
   }
}
1

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.