0

I get data from array.

This is my result:

0,0,1602907512,1602907764,1602907512,1602907764,60000000,90000000

I need like that:

0,1602907512,1602907512,60000000
0,1602907764,1602907764,90000000

How can I make with JavaScript?

Return code:

     function getPlayerDeposit(address addr) public view returns (uint256[] memory, uint256[] memory, uint256[] memory, uint256[] memory) {
        Deposit[] memory invests = users[addr].deposits;
        uint256[] memory boxIds = new uint256[](invests.length);
        uint256[] memory baseTimes = new uint256[](invests.length);
        uint256[] memory lastCollectedTimes = new uint256[](invests.length);
        uint256[] memory values = new uint256[](invests.length);

        uint256 i = 0;
        while (i < invests.length){
            Deposit memory invest = invests[i];
            boxIds[i] = 0;
            baseTimes[i] = invest.depositTime;
            lastCollectedTimes[i] = invest.lastDepositTime;
            values[i] = invest.amount;
            i++;
        }
        return (boxIds, baseTimes, lastCollectedTimes, values);
    }

thanks

3
  • Show us your return solidity function, maybe there is better to achieve that. Oct 17 '20 at 4:41
  • Very simple: const arr = [0,1602907512,1602907512,60000000 0,1602907764,1602907764,90000000];. Oct 17 '20 at 7:21
  • @MiroslavNedelchev check my code above. goodvibration I don't understand your question. Like that I want to get yes, but how with js?
    – chris_5
    Oct 17 '20 at 8:26
1

The question is pure javascript one and has nothing to do with Ethereum, but I will give you the answer, because you're a new contributor and I hope next time you will pay more attention to keep this forum only Ethereum related.

var arr = [0, 0, 1602907512, 1602907764, 1602907512, 1602907764, 60000000, 90000000];
var outcomeArr = [];

var tempCounter = 0;
var tempOutcomeArrIndex = 0;
var propertiesEachObject = 4;
var tempCounterMax = arr.length / propertiesEachObject;
var tempArr = [];

if (tempCounterMax != 0) {
    for (var i = 0, len = arr.length; i < len; i+=1) {
        if (tempCounter % tempCounterMax == 0) {
            for ( var y = 0; y < tempCounterMax; y+=1) {
                if (outcomeArr[y] == undefined) {   
                    outcomeArr[y] = [];
                }
                
                outcomeArr[y][tempOutcomeArrIndex] = arr[i + y];
            }
            tempOutcomeArrIndex+=1;
            
            tempArr = [];
        }
        tempArr.push(arr[i]);
        tempCounter+=1;
    }

    console.log(outcomeArr, 'outcomeArr');
}

What matters here the most is the variable propertiesEachObject, this variable is defined by how many properties you want your smart contract method to return for each player.

@Alireza's answer is also an solution, but you need to make more than one external call. If you want to show the player data for 10 different players in a list then you have 10 calls, because each call returns the data for only player. It depends a lot what you want to do with the data.

4
  • Thanks for your comprehensive and correct answer. It is important to mention this point that the quetioner, @chris_5, made a mistake when it was editing its question. It wrote its question as if it wants0,1602907512,1602907512,60000000 0,1602907764,1602907764,90000000 , Whereas, when i intended to edit its question i understood it wants two outputs so that one includes 0,1602907512,1602907512,60000000 and other includes 0,1602907764,1602907764,90000000. I edited its question to the correct form but @chris_5 didn't see my edition yet. My answer is based on this question. Good Luck.
    – Alireza
    Oct 17 '20 at 9:51
  • Cool, happy to help! Please accept this question with the green check and mark it as useful so others that struggles to find it easier. Oct 17 '20 at 9:54
  • hey i will not open new thread because i'm sure you can help me. your code works, but the only problem is because not works if use var contract = await tronWeb.contract().at("TLf3dYRFZn8Tf9r2PrWENeG4kRmNSPakSL"); var result = await contract.getPlayerDeposit("THmDSRv9PJPccvJL8Aj8PAND5bcSFCp4fF").call(); I used console.log(result) and display ok, but when I use this code in your code I' will get nothing..
    – chris_5
    Oct 17 '20 at 13:32
  • @chris_5 I think you should first approve question edition that done by me. By this way others can understand your require correctly.
    – Alireza
    Oct 17 '20 at 17:46
1

In JavaScript, We suppose you defined your contract object in a variable called `MyContract'. By the below statement, in an async function, you can get array result:

let MyContractOutput= await MyContract.methods.getPlayerDeposit(<Give an address>).call();

By this way, you get the result like this:

0,1602907512,1602907512,60000000

In order that you get a result like this:

0,1602907512,1602907512,60000000
0,1602907764,1602907764,90000000

,as you mentioned in your question, you need to use the above statement twice. I mean :

MyContract.methods.getPlayerDeposit(<Give an address>).call();

There are two ways to do this, One) you can run the mentioned statement twice and one after one other. Two) you can overwrite the statement, something like this:

let MyContractOutput1= await MyContract.methods.getPlayerDeposit(<Give an address>).call();
let MyContractOutput2= await MyContract.methods.getPlayerDeposit(<Give an address>).call();

Which method you use depends on which method fits the code you wrote. I did not fully understand what your contract does because the whole code of your contract was not provided in the question. So, please yourself recognize which method is appropriate to you.

Good Luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.