-1

here is my first array

let specificCoin = ["Ethereum","Bitcoin"]

and here is the second one

 let coins =[
    {
        name: 'Bitcoin',
    },
    {
        name: 'Ethereum',
    },
    {
        name: 'Solana',
    },
    {
        name: 'BinanceCoin',
    }]

i wondering how can i filter the coins array with includes in specificCoin array something like that:

coins.filter(a => { return a.name.includes(specificCoin) })

result: [{name: 'Ethereum',},{name: 'Bitcoin'}]

and i also would like to know if there are two same objects value how can i get both in it, using this code

let dataAssest = data.map(b => b.assestSymbol.toLowerCase())
    let filtered = coins.filter(coin => dataAssest.includes(coin.symbol))

image of result: Wrong result

but that ain’t work, i hope you guys can give a good solution :)

Ram John
  • 11
  • 3

1 Answers1

1

Other way around

let specificCoin = ['ETH', 'BTC'].map(symbol => symbol.toLowerCase())

let coins = [{ id: 'Bitcoin', symbol: 'btc' }, { id: 'Ethereum', symbol: 'eth' }, { id: 'Solana', symbol: 'sol'}, { id: 'BinanceCoin', symbol: 'bnb'  }];

const filtered = coins.filter(coin => specificCoin.includes(coin.symbol.toLowerCase()))

console.log(filtered)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • what about if i have same object value in array, how can i includes both ? – Ram John Jan 30 '22 at 15:11
  • Not sure what you mean – mplungjan Jan 30 '22 at 15:21
  • `let coins =[{name: 'Bitcoin'},{name: 'Ethereum',},{name:'Ethereum',}]` with that array your code return to me `[{name: 'Bitcoin',},{name: 'Ethereum'}]`, how can i get both "Ethereum" after filter. like this result: `[{name: 'Bitcoin',},{name: 'Ethereum'}, {name: 'Ethereum'}]` – Ram John Jan 30 '22 at 15:23
  • Try my code with both - it works – mplungjan Jan 30 '22 at 15:24
  • i tried that ain’t work it’s not return to me twice if there are same objects, i think that need to to if it's already includes: include again but i have no idea how to make it correct – Ram John Jan 30 '22 at 15:36
  • What is expected output? I’ll look later – mplungjan Jan 30 '22 at 15:37
  • I added a picture and code that i tried to my question here you can see it above, thanks you sir – Ram John Jan 30 '22 at 15:44
  • I do not understand why you have btc twice in the first array. My code will work on testing the symbol in the second array against the first array. Can you show the actual input array and the expected output array? You code does not match the image – mplungjan Jan 30 '22 at 17:16
  • the first array that you see on image it is the first line of code and the second one it is the second line of code... – Ram John Jan 30 '22 at 18:09