基于字符串搜索的对象的更新数组:JavaScript

发布于 2025-01-24 05:24:12 字数 1052 浏览 1 评论 0 原文

我有一个将字符串作为输入的函数,并且基于此输入,我需要更新结果数组。每当输入中存在字符串“或“”时,我需要将其余项目除外,或者通过为输入中的每个项目创建对象来更新结果。当字符串中存在“和”时,我只需要将整个输入作为结果。

function checkValid(str) {
   let search = null;
   let textContent = str.split(" ");
   const orArr = ["OR", "or", "Or", "oR"];
   if (textContent.includes("OR") || textContent.includes("or")) {
      const removedOR = textContent.filter((item) => orArr.includes(item));
      search = removedOR.map((item) => {
        return { type: "search", value: item};
      });
    } else {
      search = { type: "search", value: str };
    }
    return [...search]
}

对于输入 1或2 ,输出应为 {类型:“搜索”,value:1} {type:“ search”,value:2}

for Input 1或2和3 ,输出应为 {类型:“搜索”,值:“ 1或2和3”}

对于输入 1或23或5 ,输出应为 {类型:“搜索”,值:1} {type:“ search”,value:23} {type:“ search”,value:5}

for Input 1和3 <<< /代码>,输出应为 {类型:“搜索”,值:“ 1和3”}

我很难编写一个函数,以检查是否在或同时检查字符串中。还让我知道是否有任何改善

I have a function that takes string as an input, and based on this input I need to update a result array . Whenever there is string "OR" is present in the input , i need to take rest of the items other than OR and update the result by creating an object for each of the item in the input. When there is "AND" present in the string, I just have to take the whole input as the result.

function checkValid(str) {
   let search = null;
   let textContent = str.split(" ");
   const orArr = ["OR", "or", "Or", "oR"];
   if (textContent.includes("OR") || textContent.includes("or")) {
      const removedOR = textContent.filter((item) => orArr.includes(item));
      search = removedOR.map((item) => {
        return { type: "search", value: item};
      });
    } else {
      search = { type: "search", value: str };
    }
    return [...search]
}

For the input 1 OR 2, output should be
{ type: "search", value: 1 } { type: "search", value: 2 }

For the input 1 OR 2 AND 3, output should be
{ type: "search", value: "1 OR 2 AND 3" }

For the input 1 OR 23 OR 5, output should be
{ type: "search", value: 1 } { type: "search", value: 23 } { type: "search", value: 5 }

For the input 1 AND 3, output should be
{ type: "search", value: "1 AND 3" }

I am having trouble writing a function for checking if AND is present in the string when there is OR as well. Also let me know if there is any betterment that can be made

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2025-01-31 05:24:12

这有效

const checkValid = string => {
  let words = string.toUpperCase().split(' ')
  if (words.includes('AND')) {
    return [{ type: 'search', value: string }]
  }
  
  words = words.filter(word => word !== 'OR')
  
  return words.map(word => ({ 
    type: 'search',
    value: word
  }))
}

This works

const checkValid = string => {
  let words = string.toUpperCase().split(' ')
  if (words.includes('AND')) {
    return [{ type: 'search', value: string }]
  }
  
  words = words.filter(word => word !== 'OR')
  
  return words.map(word => ({ 
    type: 'search',
    value: word
  }))
}
看海 2025-01-31 05:24:12

尝试此

function checkValid(str) {
  // convert whole input into an upper case so that we can easily compare it without having any seperate OR array.
  let textContent = str.toUpperCase().split(' ');

  // If any 'AND' found in a string this function will return the output.
  if (textContent.includes('AND')) {
    return [{ type: 'search', value: str }]
  }

  // Logic if 'OR' found in the input string
  if (textContent.includes('OR')) {
    textContent = textContent.filter(item => item !== 'OR')
    return textContent.map(item => {
      return {
        type: 'search',
        value: item
      }
    })
  }
}

console.log(checkValid('1 AND 2 OR 3'));

Try this :

function checkValid(str) {
  // convert whole input into an upper case so that we can easily compare it without having any seperate OR array.
  let textContent = str.toUpperCase().split(' ');

  // If any 'AND' found in a string this function will return the output.
  if (textContent.includes('AND')) {
    return [{ type: 'search', value: str }]
  }

  // Logic if 'OR' found in the input string
  if (textContent.includes('OR')) {
    textContent = textContent.filter(item => item !== 'OR')
    return textContent.map(item => {
      return {
        type: 'search',
        value: item
      }
    })
  }
}

console.log(checkValid('1 AND 2 OR 3'));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文