Javascript 如何使搜索不区分大小写

Javascript 如何使搜索不区分大小写,javascript,angular,typescript,search,Javascript,Angular,Typescript,Search,我有一个数据名列表,我想在其中搜索。无论情况如何,它都应该给出结果 这就是我所拥有的: public groups = [{ name: '"Grx-1"', selected: false }, { name: '"Grx-man-2"', selected: false }, { name: '"Grx-up-3"', selected: false }]; queryGroups(groupName) { thi

我有一个数据名列表,我想在其中搜索。无论情况如何,它都应该给出结果

这就是我所拥有的:

public groups = [{ name: '"Grx-1"', selected: false }, { name: '"Grx-man-2"', selected: false }, { name: '"Grx-up-3"', selected: false }];

queryGroups(groupName) {
        this.groups = this.totalGroupsList.filter((group) => {
            if (group.userId.includes(groupName) || group.dps.includes(groupName) || group.sourceType.includes(groupName)) {
                return true;
            } else {
                let isRole = false;
                group.role.forEach((role) => {
                    if (role.name.includes(groupName)) {
                        isRole = true;
                        return;
                    }
                });
                if (isRole === false) {
                    return false;
                } else {
                    return true;
                }
            }
        });
    }
如果我搜索“Grx”,我会得到所有结果。我希望如果我搜索“grx”,我应该得到所有结果。

您可以使用


您必须使用多个搜索方法:

  queryGroups(groupName: string) {
    this.groups = this.totalGroupsList.filter((group) => {
      let isExist = this.searchFunc(groupName, group.userId)
        || this.searchFunc(groupName, group.dps)
        || this.searchFunc(groupName, group.sourceType)
      if (isExist) {
        return true;
      } else {
        let isRole = false;
        group.role.forEach((role) => {
          if (this.searchFunc(groupName, role.name)) {
            isRole = true;
            break;
          }
        });
        return isRole !== false;
      }
    });
  }

  private searchFunc(searchKey, searchTarget): boolean {
   if(!searchKey) {
      return false;
    }
    return (searchTarget.toLocaleUpperCase().includes(searchKey.toLocaleUpperCase())) ||
      (searchTarget.toUpperCase().includes(searchKey.toUpperCase())) ||
      (searchTarget.includes(searchKey.toLocaleUpperCase())) ||
      (searchTarget.includes(searchKey.toUpperCase())) ||
      (searchTarget.toLocaleUpperCase().includes(searchKey)) ||
      (searchTarget.toUpperCase().includes(searchKey)) ||
      (searchTarget.includes(searchKey))
  }

  queryGroups(groupName: string) {
    this.groups = this.totalGroupsList.filter((group) => {
      let isExist = this.searchFunc(groupName, group.userId)
        || this.searchFunc(groupName, group.dps)
        || this.searchFunc(groupName, group.sourceType)
      if (isExist) {
        return true;
      } else {
        let isRole = false;
        group.role.forEach((role) => {
          if (this.searchFunc(groupName, role.name)) {
            isRole = true;
            break;
          }
        });
        return isRole !== false;
      }
    });
  }

  private searchFunc(searchKey, searchTarget): boolean {
   if(!searchKey) {
      return false;
    }
    return (searchTarget.toLocaleUpperCase().includes(searchKey.toLocaleUpperCase())) ||
      (searchTarget.toUpperCase().includes(searchKey.toUpperCase())) ||
      (searchTarget.includes(searchKey.toLocaleUpperCase())) ||
      (searchTarget.includes(searchKey.toUpperCase())) ||
      (searchTarget.toLocaleUpperCase().includes(searchKey)) ||
      (searchTarget.toUpperCase().includes(searchKey)) ||
      (searchTarget.includes(searchKey))
  }