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

274
Visualizações
How to check if there are two (or more) forward slashes in a string of an Array item with Javascript

I have below response from a graphql query:

"menu": [{
    "url": ""
  },
  {
    "url": "/"
  },
  {
    "url": "/item/child-item"
  }
]

I need a Boolean variable and check if there is an item in the Array with two (or more) forward slashes /. So it has a child-item in the path like "/item/child-item"

const hasChild = data.menu.map((item) => item.url.includes('// What to do here?'));
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You do not need map(). map() is when you want to transform an array into another using a function.

You need some(), which returns true if any one of the array items fulfills a condition.

You can use that along with split() to count the number of occurences of a particular character in the string.

const data = { "menu": [
      {
        "url": ""
      },
      {
        "url": "/"
      },
      {
        "url": "/item/child-item"
      }
    ] }

const hasChild = data.menu.some((item) => item.url.split('/').length - 1 > 1);

console.log(hasChild);

In the above code with split(), /a/b/c becomes the array ['','a','b','c']. So you alway get the number of / + 1.

about 4 years ago · Juan Pablo Isaza Relatório

0

The .map() method returns an array, as you're after a boolean, you need to use something different. The .some() method can instead be used, which will return true as soon as the callback you pass it returns true. The .some() will iterate through all of your objects within your array until your callback returns true. You can use the .replace() method to remove any occurrences of characters that are not / and grab the length to check if it appears two or more:

const hasChild = data.menu.some(menuItem => menuItem.url.replace(/[^/]/g, '').length >= 2);

See runnable example below:

const data = {
  "menu": [{
      "url": ""
    },
    {
      "url": "/"
    },
    {
      "url": "/item/child-item"
    }
  ]
};

const hasChild = data.menu.some(menuItem => menuItem.url.replace(/[^/]/g, '').length >= 2);
console.log(hasChild); // true

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array#some and as for the test, you can split the url by / and if the resulting array has more than 2 elements, then there are at least two / in the string.

const input = {"menu": [ { "url": "" }, { "url": "/" }, { "url": "/item/child-item" } ] },

      output = input.menu.some(({url}) => url.split("/").length > 2);
      
console.log( output );

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