Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!