Solidity & truffle: get elements from strings array

Ethereum Asked by Paulo N. Carrillo Peña on December 5, 2020

i’m glad to follow this forum, it is very helpful.
I’m using truffle to develop contracts and i just found that dynamic arrays are not yet suported, for that reason i was using string arrays with a fixed lenght and i found that the element with index 0 is not showing anything and last one says undefined. do you know ay reason of that?, do i have to get elements in a different way??

This is the function in solidity:

pragma solidity ^0.4.21;
pragma experimental ABIEncoderV2;

contract Example {
    function getStringArray() public returns (string[]) {
        string[] memory resultStr = new string;
        resultStr[0] = "355555555";
        resultStr[1] = "2";
        resultStr[2] = "8888";
        return resultStr;

I also did a test with truffle like this:

in javascript i test getting elements one by one and i got this:

var ExampleCont = artifacts.require("Example");

contract('ExampleCont', async(accounts) => {
    before(async () => {
    ExampleContract = await;
    it('Upload File', async () => {
        let stringArray = await;
        console.log("list size=" + stringArray.lenght+"0="
              + stringArray[0] + "1="+stringArray[0]+", 2=" + stringArray[1] + "3=" + stringArray[2]);

list size=3, value 0= , 1=355555555, 2=2, 3=undefined

any idea about what’s happening in this case?, how do i have work well with string arrays to get all elements?

2 Answers

I used ABIEncoderv2 with solidity v0.5.0, and below code works perfectly.

function _getStringArray() public view returns (string[] memory) {
    string[] memory resultStr = new string[](3);
    resultStr[0] = "355555555";
    resultStr[1] = "2";
    resultStr[2] = "8888";
    return resultStr;

JavaScript starts here. I have used "web3 ^1.0.0-beta.36". I have tested in Kovan network, so use your kovan INFURA configuration to see it works. It's what can I do for now.

const ABI = [{"constant":true,"inputs":[],"name":"_getStringArray","outputs":[{"name":"","type":"string[]"}],"payable":false,"stateMutability":"view","type":"function"}];
const infuraUrl = "INFURAURL";
const infuraKey = "INFURAKEY";
const web3 = new Web3(infuraUrl+infuraKey)

  <div id ="test"></div>,

function integrateContract() {
  return new Promise(function (resolve, reject) {
      var contract = new web3.eth.Contract(ABI, "0x9fb7ba497a7572cc3234c5d0ae4dddc08d7bc1c0");

function getList(){
  integrateContract().then(instance => { 
    instance.methods._getStringArray().call().then(res=> {



Answered by Mehmet Doğan on December 5, 2020

I believe this is something not available in Solidity yet (having array of string). Because string in solidity is array of bytes, and now if you see the treatment of string[] becomes 2 d array bytes[][] and that's a limitation with Solidity as of now.

Answered by Anil Singh on December 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP