Skip to content

Instantly share code, notes, and snippets.

@nkabrown
Last active September 10, 2015 14:13
Show Gist options
  • Save nkabrown/30317612b64cee955346 to your computer and use it in GitHub Desktop.
Save nkabrown/30317612b64cee955346 to your computer and use it in GitHub Desktop.
Reusable function to create data agnostic select filters
// on function call autocreate data agnostic filters
function createFilter(name, data) {
var items = [];
for (i in data) {
items.push(data[i][name]);
}
var list = items.filter(uniqueValues);
list.sort();
$.each(list, function(i, item) {
$('#filter-' + name).append($('<option class="options">' + item + '</option>').val(item));
});
// return only unique values
function uniqueValues(value, index, self) {
return self.indexOf(value) === index;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment