Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何删除此for循环并改用array.some函数?_Arrays_Typescript_Functional Programming - Fatal编程技术网

Arrays 如何删除此for循环并改用array.some函数?

Arrays 如何删除此for循环并改用array.some函数?,arrays,typescript,functional-programming,Arrays,Typescript,Functional Programming,我有以下课程。我想将filterOutEmails函数转换为使用数组。一些而不是当前代码 export class UsertableComponent { dataSource: MatTableDataSource<TrialUser>; createTableFromServer = (data: TrialUsers[], emailDomains:string) => { this.dataSource = new MatTableDataSour

我有以下课程。我想将filterOutEmails函数转换为使用
数组。一些
而不是当前代码

export class UsertableComponent {
  dataSource: MatTableDataSource<TrialUser>;


  createTableFromServer = (data: TrialUsers[], emailDomains:string) => {
    this.dataSource = new MatTableDataSource(data);
    this.dataSource.filterPredicate = this.filterOutEmails
  } 

  filterOutEmails = (row: TrialUser, emailDomains: string): boolean => {
    const listofFilters = emailDomains.split(',');
    for (let i = 0; i < listofFilters.length; i++){
      if (row.email.toLowerCase().includes(listofFilters[i].trim())){
        return true;
      };
    }
    return false;
  }
}


将“determineIfRowSHouldBeShown”作为内联fat arrow函数使用是可行的,但我希望将函数定义分开。

如果使用arrow函数
,即使作为参数传递,此
也会在词汇上绑定:

// If you're gonna abuse `this` I wouldn't put the function in a class as it's really confusing for `this` to not be the instance. You also can't use arrow functions in this case as they prevent rebinding the context.
function determineIfRowShouldBeShown(domain: string): boolean {
    return !this.email.toLowerCase().includes(domain.trim());
}

export class UsertableComponent {
  dataSource: MatTableDataSource<TrialUser>;

  createTableFromServer = (data: TrialUsers[], emailDomains:string) => {
      this.dataSource =  new MatTableDataSource(data);
      this.dataSource.filterPredicate = this.filterOutEmails
  }
  // I don't think this is a good name since it doesn't actually filter anything.
  filterOutEmails(row: TrialUser, emailDomains: string): boolean{
    return emailDomains.split(',').some(determineIfRowShouldBeShown, row);
  }
}
使用咖喱/部分应用程序(我的首选)


如果使用箭头函数
,则即使作为参数传递,此
也会受到词汇约束:

// If you're gonna abuse `this` I wouldn't put the function in a class as it's really confusing for `this` to not be the instance. You also can't use arrow functions in this case as they prevent rebinding the context.
function determineIfRowShouldBeShown(domain: string): boolean {
    return !this.email.toLowerCase().includes(domain.trim());
}

export class UsertableComponent {
  dataSource: MatTableDataSource<TrialUser>;

  createTableFromServer = (data: TrialUsers[], emailDomains:string) => {
      this.dataSource =  new MatTableDataSource(data);
      this.dataSource.filterPredicate = this.filterOutEmails
  }
  // I don't think this is a good name since it doesn't actually filter anything.
  filterOutEmails(row: TrialUser, emailDomains: string): boolean{
    return emailDomains.split(',').some(determineIfRowShouldBeShown, row);
  }
}
使用咖喱/部分应用程序(我的首选)


该问题中建议答案的可能重复对于typescript和
some
函数不起作用。该问题中建议答案的可能重复对于typescript和
some
函数不起作用。typescript编译器不喜欢这样:
错误app/trialusers/usertable/usertable.component.ts:65:18-错误TS2339:类型“UsertableComponent”上不存在属性“email”。回来!this.email.toLowerCase()包括(domain.trim())
UserTableComponent是这个函数所在的类。我想我可能误解了这些方法所在的类以及它们之间的关系。你能提供更多的上下文吗?更新了上下文。谢谢。我一直在寻找咖喱的解决方案。令人惊讶的是,如果不强制转换thisArg,使用bind会导致相同的编译器错误。typescript编译器不喜欢这样:
app/trialUserTable/usertable.component.ts:65:18中的错误-错误TS2339:类型“UsertableComponent”上不存在属性“email”。回来!this.email.toLowerCase()包括(domain.trim())
UserTableComponent是这个函数所在的类。我想我可能误解了这些方法所在的类以及它们之间的关系。你能提供更多的上下文吗?更新了上下文。谢谢。我一直在寻找咖喱的解决方案。令人惊讶的是,如果不在thisArg上强制转换,使用bind会导致相同的编译器错误。
...
filterOutEmails(row: TrialUser, emailDomains: string){
  emailDomains.split(',').some(determineIfRowShouldBeShown.bind(row));
}

const determineIfRowShouldBeShown = (row: TrialUser) => (domain: string): boolean => !row.email.toLowerCase().includes(domain.trim());
...
filterOutEmails(row: TrialUser, emailDomains: string){
  emailDomains.split(',').some(determineIfRowShouldBeShown(row));
}