Skip to content
Advertisement

filter Async data in Angular [closed]

(5) [{…}, {…}, {…}, {…}, {…}]
0: {token_address: '0x100c6e87e7a12a49b8e3af3c2db8feac20ac473f', name: 'solana', symbol: 'SOL', logo: null, thumbnail: null, …}
1: {token_address: '0xf3f45420122dad3c89abf132ee4c0930aefed0b0', name: 'Bitcoin', symbol: 'BTC', logo: null, thumbnail: null, …}
2: {token_address: '0x328eb9e5e37af976b00324f90a84d42842f2fc4e', name: 'Ethereum', symbol: 'ETH', logo: null, thumbnail: null, …}
3: {token_address: '0x2b1544ec925b5b475c0f019fd9738375b4888330', name: 'Ripple', symbol: 'XRP', logo: null, thumbnail: null, …}
4: {token_address: '0xac6b8aaf41e9bbc4b66d4870b0daa5422dca9ffa', name: 'Tron', symbol: 'TRX', logo: null, thumbnail: null, …}

I want to filter only coins whos symbol is BTC ETH and SOL into a separate array.(I have set this above data = walletData).I’m trying to do this

specificTokens(){
    const result = this.walletData.filter(item=>{
     if(item.symbol == "ETH"){
    console.log(item)
     }
    });
   }

I get the result

{token_address: '0x328eb9e5e37af976b00324f90a84d42842f2fc4e', name: 'Ethereum', symbol: 'ETH', logo: null, thumbnail: null, …}
balance: "30000000000000000000"
decimals: 18
logo: null
name: "Ethereum"
symbol: "ETH"
thumbnail: null
token_address: "0x328eb9e5e37af976b00324f90a84d42842f2fc4e"
[[Prototype]]: Object

But when I try to

specificTokens(){
    const result = this.walletData.filter(item=>{
     if(item.symbol == "ETH" && item.symbol == "BTC"){
    console.log(item)
     }
    });
   }

I don’t get anything.What am I doing wrong here, and how do I get the desired result.Thankyou

Advertisement

Answer

You are using “&&” (and) instead of “||” (or)

change to:

  specificTokens() {
    const result = this.walletData.filter(item => {
      if (item.symbol == "ETH" || item.symbol == "BTC") {
        console.log(item)
      }
    }
  }

Edit:

  specificTokens() {
    const result = this.walletData.filter(item => item.symbol == "ETH" || item.symbol == "BTC");
  }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement