[Vuejs]-Filter Array by keyword

0👍

One problem I can see with your approach.

You assign the filtered results to this.bets which means it will keep filtering on previous filters. You should assign the filtered result to a new variable.

Here is a working example.

let bets = [
  {
    bet_amount: "0.0000001",
    bet_id: "fe5f40-3ea93b",
    client_seed: "hash",
    username: "graham"
  },
  {
    bet_amount: "0.0000002",
    bet_id: "kjs93f-sflll0",
    client_seed: "hash",
    username: "roller"
  },
  {
    bet_amount: "0.0000003",
    bet_id: "099s99-lpap11",
    client_seed: "hash",
    username: "card"
  }
]

function searchResults() {
  //if (!this.searchTable) this.fetch_bet_data();

  const searchableKeys = ["username", "bet_id"];

  let results = bets.filter(bet => {
    return searchableKeys.some(key => {
      return bet[key].toLowerCase().includes(document.getElementById("theInput").value);
    });
  });
  console.log(results);
}
<input id="theInput" type="text" v-model="searchTable" class="form-control" placeholder="Search">
<div class="input-group-btn">
  <button class="btn btn-success" onClick="searchResults()" type="submit">
         <i class="glyphicon glyphicon-search">Search</i>
      </button>
</div>

Leave a comment