Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
Update array of objects based on string search : Javascript

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

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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
  }))
}
about 4 years ago · Juan Pablo Isaza Relatório

0

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'));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda