Example 1: angular array filter typescript
ngOnInit() {
this.booksByStoreID = this.books.filter(
book => book.store_id === this.store.id);
}
Example 2: how the filter() function works javascript
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const filter = arr.filter((number) => number > 5);
console.log(filter); // [6, 7, 8, 9]
Example 3: array filter
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
Example 4: filter array by keyword
var data = [
{email: "[email protected]",nama:"User A", Level:"Super Admin"},
{email: "[email protected]",nama:"User B", Level:"Super Admin"},
{email: "[email protected]",nama:"User C", Level:"Standart"},
{email: "[email protected]",nama:"User D", Level:"Standart"},
{email: "[email protected]",nama:"User E", Level:"Admin"},
{email: "[email protected]",nama:"User F", Level:"Standart"}
];
var filter = "Level";
var keyword = "Standart";
var filteredData = data.filter(function(obj) {
return obj[filter] === keyword;
});
console.log(filteredData);
Example 5: filter items javascript
// Filter items
function filterItems(e) {
// Convert to lowercase
var text = e.target.value.toLowerCase();
// Get lis
var items = itemList.getElementsByTagName('li');
// Convert to an array
Array.from(items).forEach(function(item) {
var itemName = item.firstChild.textContent;
if (itemName.toLowerCase().indexOf(text) != -1) {
item.style.display = 'block';
} else {
item.style.display = 'none';
}
});
}