Home > Software design >  How to find exact string
How to find exact string

Time:12-06

I have an array:

and I trying to find the exactly string value with this

It works fine even if you are looking for "b" or "bt" but I would like to get result only when i'm looking for "btc" (exactly string value)

when I look for "b" or "bt" I want to get no result

let coinsArray = [{
  symbol: "btc"
}]

let newStatus = coinsArray.filter(coin => coin.symbol.includes($(".InpSearchCoin").val()))

console.log(newStatus)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="InpSearchCoin" value="btc" />
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Don't use includes

let coinsArray = [{
  symbol: "btc"
}]

$(".InpSearchCoin").on("input", function() {
  let symbol = coinsArray
    .filter(({symbol}) => symbol === this.value.toLowerCase())
  console.log(symbol)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="InpSearchCoin" value="" />
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related