0

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?'));
3
  • 1
    Includes // or starts with //? Commented May 17, 2022 at 11:12
  • 1
    Or includes two / but not necessarily together? (Your third example.) With a question like this, it's always useful to show examples that match and don't match and to say specifically which are which. Commented May 17, 2022 at 11:13
  • yeah sorry, like the third example Commented May 17, 2022 at 11:15

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

@T.J.Crowder you say replace() is preferable because of the extra space my code uses?
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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.