Typeahead Bloodhound Filter

Filter a Bloodhound dataset dynamically

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>
<div id="default-suggestions">
  <input class="typeahead" type="text" placeholder="NFL Teams">
</div>

JavaScript

var nflTeams = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('team'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  identify: function(obj) { return obj.team; },
  //local: {'Detroit Lions': 'Detroit Lions', 'Green Bay Packers': 'Green Bay Packers', 'Chicago Bears': 'Chicago Bears'},
  local: [
{ "team": "San Francisco 49ers" },
{ "team": "Chicago Bears" },
{ "team": "Cincinnati Bengals" },
{ "team": "Buffalo Bills" },
{ "team": "Denver Broncos" },
{ "team": "Cleveland Browns" },
{ "team": "Tampa Bay Buccaneers" },
{ "team": "Arizona Cardinals" },
{ "team": "San Diego Chargers" },
{ "team": "Kansas City Chiefs" },
{ "team": "Indianapolis Colts" },
{ "team": "Dallas Cowboys" },
{ "team": "Miami Dolphins" },
{ "team": "Philadelphia Eagles" },
{ "team": "Atlanta Falcons" },
{ "team": "New York Giants" },
{ "team": "Jacksonville Jaguars" },
{ "team": "New York Jets" },
{ "team": "Detroit Lions" },
{ "team": "Green Bay Packers" },
{ "team": "Carolina Panthers" },
{ "team": "New England Patriots" },
{ "team": "Oakland Raiders" },
{ "team": "St.Louis Rams" },
{ "team": "Baltimore Ravens" },
{ "team": "Washington Redskins" },
{ "team": "New Orlean Saints" },
{ "team": "Seattle Seahawks" },
{ "team": "Pittsburgh Steelers" },
{ "team": "Houston Texans" },
{ "team": "Tennesse Titans" },
{ "team": "Minnesota Viking" }
]
  //prefetch: 'https://twitter.github.io/typeahead.js/data/nfl.json'
});

function nflTeamsWithDefaults(q, sync, async) {
  if (q === '') {
    sync(nflTeams.get('Detroit Lions', 'Green Bay Packers', 'Chicago Bears'));
  }

  else {
    nflTeams.search(q, sync);
  }
}

$('#default-suggestions .typeahead').typeahead({
  minLength: 0,
  highlight: true
},
{
  name: 'nfl-teams',
  display: 'team',
  source: nflTeamsWithDefaults
});