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

245
Visualizações
Array some(). How can I find the index of the true match found?

I've created the example below to illustrate what I'm trying to do. Essentially I wanted to get the array index of the true match and return a custom response based on which end point regex was matched.

Is this possible with array some()? Or is there a different method?

// Simulate an http request
const reqUrl = "http://127.0.0.1/app/api/v1/customers/999999";

// Parse the parts of a url
const url = new URL(reqUrl);

// Get url pathname and Decode it, if encoded
const urlPathNameDecoded = decodeURIComponent(url.pathname);

/* /app/api/v1/customers/999999 */
console.log(urlPathNameDecoded);

// Create list of regular expressions to match against url pathname decoded
const regexList = [
  /apple/,
  /^\/app\/api\/v1\/customers\/([1-9]{1}[0-9]{0,8})\/?$/, // Match -> /app/api/v1/customers/999999
  /orange/,
  /^\/app\/api\/v1\/projects\/([1-9]{1}[0-9]{0,8})\/?$/, //  Match -> /app/api/v1/projects/999999
  /pear/,
  /^\/app\/api\/v1\/vendors\/([1-9]{1}[0-9]{0,8})\/?$/, //   Match -> /app/api/v1/vendors/999999
  /strawberry/,
];

// Test if at least one element in the array passes the test implemented by the provided function
const isMatch = regexList.some(regex => regex.exec(urlPathNameDecoded));

console.log("isMatch:", isMatch);

about 4 years ago · Juan Pablo Isaza
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