I am using below code to retrieve item from an array.
How to refactor this to use .find
instead of using for
loop to find the item.
I am using angular 8
.
HTML:
<input #searchInput matInput (keyup)="updateFilter($event.target.value)" autocomplete="off"
placeholder="Narrow your search with keywords (e.g. 'open Now')">
export class aListComponent implements OnInit {
rows = [];
temp = [];
updateFilter(val) {
const value = val.toString().toLowerCase().trim();
const count = 4;
const keys = Object.keys(this.temp[0]);
this.rows = this.temp.filter((item) => {
for (let i = 0; i < count; i++) {
if (
(item[keys[i]] &&
item[keys[i]].toString().toLowerCase().indexOf(value) !== -1) ||
!value
) {
return true;
}
}
});
}
}
See this sample
[1]: https://stackblitz.com/edit/angular-ngx-datatables-filter-all-columns
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…