Solidity – How to Create Array of Multiple Data Types

contract-designcontract-developmentsoliditytruffle-contractweb3js

Since struct cannot be output from solidity to web3js,
I'm using arrays, I could get single datatype array in the below code(userAddress), but I'm not able to take multiple value in array(userName,userTarget).Any suggestions howto would be great

Solidity:

struct Person
    {
        address userAddress;
        string userName;
        uint userTarget;
    }
     mapping (uint => person) Persons;
     uint[] public personAccounts;
     uint incr = 0;

     function setDetailse(string  data, uint target) public
    {
        uint icr = incr++;
        address _address = msg.sender;
        var person = persons[icr];

        person .userAddress = _address;
        person .userName= _vote;
        person .userTarget = target;
        person Accounts.push(icr) -1;
    }

   function getPerson() view public returns(uint[])
   {
       uint256[] memory data =  new uint256[](11);
       for(uint i = 1; i<=10 ;i++)
        {
           data[i]=personAccounts[i];
        }
       return data;
   }

web3js:

GetDetails: function(Data, account) {
  event.preventDefault();
  var dataInstance;

  web3.eth.getAccounts(function(error, accounts) {
    if (error) {
      console.log(error);
    }

    var account = accounts[0];

    App.contracts.Details.deployed().then(function(instance) {
      dataInstance= instance;
      return dataInstance.getPerson();
    }).thenfunction((numbers => {
      const [ nums ] = numbers;
      for (var i = 0; i <nums.length; i++) 
    {
          console.log("address: "+nums[i]);
      }
  })).catch(function(err) {
      console.log("error executed");
    });
  });

Best Answer

One can not create an array of multiple data types. An array must contain elements of the same data type. As one can not return a struct from solidity function you cannot use an array of struct. Refer this question for how to return multiple data types from a solidity function.