0

I have the following code with a recursive function which I would like to transform to recursive arrow function:

const hasAccess = menuSections.some(function s(x) {
  if (x.link === route.routeConfig.path) {
    return true;
  }

  if (x.sections) {
    return (x.sections.some(s));
  }

  return false;
});

Any idea on how to do it?

2
  • 2
    Why? The function doesn't use this, arguments or anything else that would be handled differently by an arrow function. It does use its own name to recurse, and arrow functions are anonymous. For your problem a function expression has benefits over an arrow function but an arrow function has no benefits at all over a function expression. Commented Oct 4, 2018 at 9:11
  • @Quentin because it's a requirement from an exercise Commented Oct 4, 2018 at 9:15

1 Answer 1

1

You could use an own function for callback and shorten the conditions for the return value.

const 
    check = x => x.link === route.routeConfig.path || x.sections && x.sections.some(check),
    hasAccess = menuSections.some(check);
Sign up to request clarification or add additional context in comments.

2 Comments

It possibly should be !!x.sections so that, per the OP, it returns a boolean rather than undefined, or null, or 0 or ….
it is a callback used in some, so any falsy value works.

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.