Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

angular - How to use ES6 instance .find instead of for

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...