JavaScript
x
7
1
(5) [{…}, {…}, {…}, {…}, {…}]
2
0: {token_address: '0x100c6e87e7a12a49b8e3af3c2db8feac20ac473f', name: 'solana', symbol: 'SOL', logo: null, thumbnail: null, …}
3
1: {token_address: '0xf3f45420122dad3c89abf132ee4c0930aefed0b0', name: 'Bitcoin', symbol: 'BTC', logo: null, thumbnail: null, …}
4
2: {token_address: '0x328eb9e5e37af976b00324f90a84d42842f2fc4e', name: 'Ethereum', symbol: 'ETH', logo: null, thumbnail: null, …}
5
3: {token_address: '0x2b1544ec925b5b475c0f019fd9738375b4888330', name: 'Ripple', symbol: 'XRP', logo: null, thumbnail: null, …}
6
4: {token_address: '0xac6b8aaf41e9bbc4b66d4870b0daa5422dca9ffa', name: 'Tron', symbol: 'TRX', logo: null, thumbnail: null, …}
7
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
JavaScript
1
8
1
specificTokens(){
2
const result = this.walletData.filter(item=>{
3
if(item.symbol == "ETH"){
4
console.log(item)
5
}
6
});
7
}
8
I get the result
JavaScript
1
10
10
1
{token_address: '0x328eb9e5e37af976b00324f90a84d42842f2fc4e', name: 'Ethereum', symbol: 'ETH', logo: null, thumbnail: null, …}
2
balance: "30000000000000000000"
3
decimals: 18
4
logo: null
5
name: "Ethereum"
6
symbol: "ETH"
7
thumbnail: null
8
token_address: "0x328eb9e5e37af976b00324f90a84d42842f2fc4e"
9
[[Prototype]]: Object
10
But when I try to
JavaScript
1
8
1
specificTokens(){
2
const result = this.walletData.filter(item=>{
3
if(item.symbol == "ETH" && item.symbol == "BTC"){
4
console.log(item)
5
}
6
});
7
}
8
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:
JavaScript
1
8
1
specificTokens() {
2
const result = this.walletData.filter(item => {
3
if (item.symbol == "ETH" || item.symbol == "BTC") {
4
console.log(item)
5
}
6
}
7
}
8
Edit:
JavaScript
1
4
1
specificTokens() {
2
const result = this.walletData.filter(item => item.symbol == "ETH" || item.symbol == "BTC");
3
}
4