TypeScript
Return only defined values from an array

by Benny Neugebauer

getBalances(asset: string): Balance[] {
  const balances: Balance[] = []

  for (const account of this.accounts.values()) {
    const balance: Balance | undefined = account.getBalance(asset)
    if (balance) balances.push(balance)
  }

  return balances
}

getBalances(asset: string): Balance[] {
  const balances: Balance[] = []

  for (const account of this.accounts.values()) {
    const balance: Balance | undefined = account.getBalance(asset)
    balances.push(balance)
  }

  return balances.filter(balance => balance)
}