[SOLVED] How to sum up balances to a totalBalance

I get the tokenbalance of all token of an address and I can calculate the $ value for each of the token, now how do I calculate the toal wallet Balance?

I do this
<td>${formatter.format(price['usdPrice'] * Moralis.Units.FromWei(data[i].balance,data[i].decimals ))}</td>
which print out the USD value of each token

But I cant figure out how to calculate totalBalance
I tried to put all balances into an array but this didnt work
any suggestions?

what problem did you have with adding them to an array?

I keep getting this error when I do this

Uncaught (in promise) RangeError: invalid array length

var balances = price[‘usdPrice’] * Moralis.Units.FromWei(data[i].balance,data[i].decimals)

var array = new Array(balances)

console.log(array)

and also when doing this

var array = new Array(price['usdPrice'] * Moralis.Units.FromWei(data[i].balance,data[i].decimals))
console.log(array)

I also created an empty array and than I put in the output inside

var balances = []

var balances = new Array(price[‘usdPrice’] * Moralis.Units.FromWei(data[i].balance,data[i].decimals))

console.log(balances)

maybe that is not the right syntax, you could make an empty array like empty_array = [] and then add the elements one by one

I need a code, i tried what you said but its not working If I do this I create an array for every value

var num = []

num.push(price['usdPrice'] * Moralis.Units.FromWei(data[i].balance,data[i].decimals))

console.log(num)

var balance = new Array(num)

console.log(balance)
empty_array = []
empty_array[0] = 1
empty_array[1] = 5
console.log(empty_array)

what is this?
I need to do all of this programatically, I dont know how many token a user has
I dont know what I should do with this

empty_array = []
empty_array[0] = 1
empty_array[1] = 5
console.log(empty_array)

can you iterate the user tokens?

I dont know what you asking me, you see what I try to do
I have the balances and I want to to sum them up, i dont get your questions dude

can you explain that some more

I don’t understand what you don’t know how to do, you say that you don’t know how to do a sum of elements?

I need to put all the USD balances into an array and than I can sum it up, like on this example, but Im stuck at putting the USD values into an array

const numbers = [15.5, 2.3, 1.1, 4.7];

function getSum(total, num) {
return total + Math.round(num);
}

document.getElementById(“demo”).innerHTML = numbers.reduce(getSum);

I posted the screenshot above, each token a user has has some type of value, some users have 5 token, some have 500, so i need to be able to combine each and every USD balance into an array in order to calculate the total USD Balance but this is where Im stuck, for some reason I cant turn the USD values into a single array and I dont know why

what is the syntax that you want to use? can you try it separately in console with some simple values to see if it works there?

I dont why you keep asking me those questions?
How more clear can I make it, when I say I want to sum up all the USD values of a users wallet?

I can write this manually but I need to have it work with my code.

Check this out

var array = []

array.push(price['usdPrice'] * Moralis.Units.FromWei(data[i].balance,data[i].decimals))

console.log(array)

function getSum(total, num) {

return total + Math.round(num);

}

document.getElementById("demo").innerHTML = array.reduce(getSum, 0);

For some reason I cant get the total Balance in USD of all the token a user hasScreenshot from 2022-02-25 15-42-28

I keep getting 5 arrays instead of 1 array with 5 values

it looks like you reset the array before adding to it, it will have only 1 element this way

yes, I know thats exactly what I keep saying, but I dont know why I can not make 1 array to put all of the values inside

Thats teh code Im using

for (let i=0; i < data.length; i++){
  //Get token price on PancakeSwap v2 BSC
  var formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
  });
const options = {
  address: data[i].token_address,
  chain: "bsc",
  exchange: "PancakeSwapv2",
};
const price = await Moralis.Web3API.token.getTokenPrice(options);


    let row = `<tr>
                    <td>${data[i].name}</td>
                    <td>${data[i].symbol}</td>
                    <td>${parseFloat(Moralis.Units.FromWei(data[i].balance,data[i].decimals)).toFixed(3)}</td>
                    <td>${formatter.format(price['usdPrice'] * Moralis.Units.FromWei(data[i].balance,data[i].decimals))}</td>
                </tr>
                </div>`
    table.innerHTML += row
}

and thats what I do to add items to the array, but this creates in my case with 5 token 5 arrays instead of 1 array with 5 values

try to initialize that array before the loop that adds those elements

I tried all of this already and its not working, no matter what I do I get 5 arrays instead of 1 array with 5 values

var array = new Array();

var array = []

array.push(price['usdPrice'] * Moralis.Units.FromWei(data[i].balance,data[i].decimals))

console.log(array)

i tired all versions of initializing an array

I mean to initialize it upper in the code, not differently.

can you please post a code snippet, lik I said I tried every version on ow to initialize an array