如果用户输入与预定义的字符串匹配,则返回true。

huangapple go评论98阅读模式
英文:

Return true if user input matches to predefined string

问题

我正在为项目开发搜索功能。我有多个具有不同标题、描述和类型的项目。我需要的是,如果项目的标题、描述或类型包含用户输入的字符/单词,该函数应返回true。

例如:

我们有3个项目:

  1. 标题 = 'foo 1',项目类型 = 'good',描述 = 'good description'
  2. 标题 = 'bar 1',项目类型 = 'bad',描述 = 'this is description',
  3. 标题 = 'foo bar',项目类型 = 'neutral',描述 = 'this is something',

用户输入是 'foo',则对于 1 和 3 返回true。
用户输入是 'foo this is',则对于 1、2 和 3 返回true。

到目前为止,我创建了以下函数:

checkProjectStrings(project: ProjectEntity, filterText: string) {
    let containTextTitle: boolean | undefined;
    let containsTextDescription: boolean | undefined;
    let containsTextType: boolean | undefined;

    const filterTextArray = filterText.split(' ');

    filterTextArray.map(filterTextString => {
        if (project.title != undefined) {
            containTextTitle = project.title.toLowerCase().includes(filterTextString.toLowerCase());
        }
        if (project.description != undefined) {
            containsTextDescription = project.description.toLowerCase().includes(filterTextString.toLowerCase());
        }
        if (project.projectType != undefined) {
            containsTextType = project.projectType.toLowerCase().includes(filterTextString.toLowerCase());
        }
    })

    if (containTextTitle || containsTextDescription || containsTextType) {
        return true;
    }
    return false;
}

我的函数存在问题,似乎总是只使用最后提供的字符串。

英文:

I working on search function for projects. I have multiple projects with different titles, description and types. What I need is that the function returns true if the project title, description or type contains characters/words from the user input.

E.g.

We have 3 projects:

  1. title = 'foo 1', projectType = 'good', description = 'good description'
  2. title = 'bar 1', projectType = 'bad', description = 'this is description',
  3. title = 'foo bar', projectType = 'neutral', description = 'this is something',

User input is 'foo', returns true for 1. and 3.
User input is 'foo this is', return true for 1. 2. and 3.

I have so far created following function

checkProjectStrings(project: ProjectEntity, filterText: string) {
        let containTextTitle: boolean | undefined;
        let containsTextDescription: boolean | undefined;
        let containsTextType: boolean | undefined;

        const filterTextArray = filterText.split(' ');

        filterTextArray.map(filterTextString => {
            if (project.title != undefined) {
                containTextTitle = project.title.toLowerCase().includes(filterTextString.toLowerCase());
            }
            if (project.description != undefined) {
                containsTextDescription = project.description.toLowerCase().includes(filterTextString.toLowerCase());
            }
            if (project.projectType != undefined) {
                containsTextType = project.projectType.toLowerCase().includes(filterTextString.toLowerCase());
            }
        })

        if (containTextTitle || containsTextDescription || containsTextType) {
            return true;
        }
        return false;
    }

The problem in my function seems that it is always working just with the last string provided

答案1

得分: 1

Your code overrides your variables on every iteration. You would need to alter your ifs to not run it if there is a match

如果在每次迭代中您的代码覆盖了变量。您需要修改条件语句以确保在有匹配项时不执行它。
if (project.title != undefined && !containTextTitle ) { ... }

You can simplify it with some

您可以通过以下方式简化它:
const filterTextArray = filterText.split(' ');

filterTextArray.some(filterTextString => {
  return (project.title && project.title.toLowerCase().includes(filterTextString.toLowerCase())) || (project.description &&
    containsTextDescription = project.description.toLowerCase().includes(filterTextString.toLowerCase())
  ) ||
  (project.projectType &&
    project.projectType.toLowerCase().includes(filterTextString.toLowerCase())
  )
})
英文:

Your code overrides your variables on every iteration. You would need to alter your ifs to not run it if there is a match

if (project.title != undefined && !containTextTitle ) { ... }

You can simply it with some

const filterTextArray = filterText.split(' ');

filterTextArray.some(filterTextString => {
  return (project.title && project.title.toLowerCase().includes(filterTextString.toLowerCase())) || (project.description &&
    containsTextDescription = project.description.toLowerCase().includes(filterTextString.toLowerCase())
  ) ||
  (project.projectType &&
    project.projectType.toLowerCase().includes(filterTextString.toLowerCase())
  )
})

答案2

得分: 0

我尝试将整个项目对象转换为单个字符串行来执行此操作。

const projectsContainingInput = projects
      .filter( project => {
        const projectLine = Object.values(project).join(' ').toLowerCase() // 例如:'facebook social media'
        return words.some(word => projectLine.includes(word))
      })

https://codepen.io/chevallm/pen/qBEpoVg

英文:

I tried to do it by turning the entire project object into a single string line

const projectsContainingInput = projects
      .filter( project => {
        const projectLine = Object.values(project).join(' ').toLowerCase() // ex. 'facebook social media'
        return words.some(word => projectLine.includes(word))
      })

https://codepen.io/chevallm/pen/qBEpoVg

huangapple
  • 本文由 发表于 2020年1月6日 23:10:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定