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
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:
MyContract.methods.read().call(function(error, result){
if(!error) {
console.log(result);
}
});
[ '1', '3', '9' ]
MyContract.read(function(error, result){
if(!error) {
console.log(result);
}
});
[ 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.
var array = [];
for(var i=0;i<result.length;i++){
array.push(result[i].toString());
}
console.log(array);
[ '1', '3', '9' ]
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);
}
});
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
.toNumber()
to each element. or you can use the map function developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… eg:array.map(x => x.toNumber());