简体   繁体   中英

How to grab the values in a nested object that has functions

i am new to js and destructing or working with objects in js... i have spent most of the day trying to access this amount object variable that in itself is nested within a balances object... i want to get the nested amount (that has the function)

this is the function im using:

const newWallet  = async () => {                                                           
   34       const keystore1 = JSON.parse(fs.readFileSync(keystorelocation, 'utf8'))              
   35       let phrase = await decryptFromKeystore(keystore1, keypasswd)                         
   36       const combowallet= new Wallet(Network.Mainnet, phrase)                               
   37       const bal = await combowallet.getAllBalances()                                       
   38       console.log("bal2:" + bal[2])                                                        
   39       console.log("bal2 address:" + bal[2].address)                                        
   40       console.log("bal2 chain:" + bal[2].chain)                                            
   41       console.log("bal2 balances[0]:" + bal[2].balances[0])                                
   42       console.log(bal[2].balances[1])                                                      
   43       const [ BTC, ETH, thor ] = bal                                                       
   44       console.log("BTC:" + BTC)                                                            
   45       console.log("ETH:" + ETH)                                                            
   46       console.log("THOR:")                                                                 
   47       console.log(thor)                                                                                                                                                                                                                                 
   48       const {chain , address, ...balance } = thor                                                                                  
   49       console.log("balance is:" + balance)                                                                                         
   50       const {balances} = balance                                                                                                   
   51       console.log("balances is:" + balances)                                                                                                                                                                         
   52                                                                                                                                                                                                                      
   53                                                                                                                                                                                                                      
   54   }                                           

here is the type:

type AllBalances = {
    chain: Chain
    address: string
    balances: Balance[] | string
  }

i de structured the object via this line

const {chain , address, ...balance } = thor 

got this

{ balances: [ { asset: [Object], amount: [Object] } ] }

{
  asset: { chain: 'THOR', symbol: 'RUNE', ticker: 'RUNE', synth: false },
  amount: {
    type: 'BASE',
    amount: [Function: amount],
    plus: [Function: plus],
    minus: [Function: minus],
    times: [Function: times],
    div: [Function: div],
    lt: [Function: lt],
    lte: [Function: lte],
    gt: [Function: gt],
    gte: [Function: gte],
    eq: [Function: eq],
    decimal: 8
  }
}

how do i get to amount -> amount

THIS IS THE FULL RESULT OF THE OUTPUT FROM THIS FUNCTION VIA CONSOLE.LOG

bal2:[object Object]
bal2 address:thor1n5u5drsxjjjcvvgrampugwzvj5zjgm6rruugf2
bal2 chain:THOR
bal2 balances[0]:[object Object]
undefined
BTC:[object Object]
ETH:[object Object]
THOR:
{
  chain: 'THOR',
  address: 'thor1n5u5drsxjjjcvvgrampugwzvj5zjgm6rruugf2',
  balances: [ { asset: [Object], amount: [Object] } ]
}
balance is:[object Object]
balances is:[object Object]

You can get the result of the amount function like this:

balance.at(0).amount.amount(); // for the first balance

If you want the amounts of all balances:

const amounts = balance.map(b => b.amount.amount());

If you need the sum of all balances (works only if amount() returns a Number :

const sum = arr => arr.reduce((ps, a) => ps + a, 0);
const totalAmounts = sum(balance.map(b => b.amount.amount()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM