3


I have a smart contract method that return an array of uint256,

but when I console.log the result I got this :

[ BigNumber { s: 1, e: 1, c: [Array] } ]

How can we convert it to array of numbers? toNumber() work for a single object

1

1 Answer 1

1

Consider a deployed contract defined as follows:

pragma solidity ^0.4.25;
contract MyContract {
    uint256[] public data;
    function write(uint256 entry) public {
        data.push(entry);
    }    
    function read() public constant returns(uint256[]) {
        return(data);
    }
}

After having called write(1), write(3) and write(9), the expected outputs are as follows, depending on which web3 version is in use:

read() with [email protected]

MyContract.methods.read().call(function(error, result){
    if(!error) {
        console.log(result);
    }
});

Outputs:

[ '1', '3', '9' ]

read() with [email protected]

MyContract.read(function(error, result){                                                                                                                                    
    if(!error) {
        console.log(result);
    }
});

Outputs:

[ BigNumber { s: 1, e: 0, c: [ 1 ] },
  BigNumber { s: 1, e: 0, c: [ 3 ] },
  BigNumber { s: 1, e: 0, c: [ 9 ] } ]

As mentioned by user mirg, the results from the [email protected] call must be individually converted into a number or string.

Conversion example:

var array = [];
for(var i=0;i<result.length;i++){
    array.push(result[i].toString());
}
console.log(array);

Outputs:

[ '1', '3', '9' ]

Appendix

array100.js ([email protected])

const Web3 = require("web3");
const address = "https://rinkeby.infura.io/";
const web3 = new Web3(address);
const provider = web3.currentProvider;
const contractAbi=[{"constant":false,"inputs":[{"name":"entry","type":"uint256"}],"name":"write","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"read","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"data","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];
const contractAddress = "0x0000000000000000000000000000000000000000";
var MyContract = new web3.eth.Contract(contractAbi, contractAddress);
MyContract.methods.read().call(function(error, result){
    if(!error) {                                                                                                                                                            
        console.log(result);
    }
});

array020.js ([email protected])

const Web3 = require("web3");
const address = "https://rinkeby.infura.io/";
const web3 = new Web3(new Web3.providers.HttpProvider(address));
const contractAbi=[{"constant":false,"inputs":[{"name":"entry","type":"uint256"}],"name":"write","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"read","outputs":[{"name":"","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"data","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];
const contractAddress = "0x0000000000000000000000000000000000000000";
var MyContract = web3.eth.contract(contractAbi).at(contractAddress);
MyContract.read(function(error, result){
    if(!error) {
        var array = [];
        for(var i=0;i<result.length;i++){
            array.push(result[i].toString());                                                                                                                               
        }
        console.log(array);
    }
});

Hope that helps.

Stay super!

/Javi

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.