Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...