jeudi 1 juillet 2021

How to pass nothing to a function using ternary operator - javascript

I was wondering if I can do the following with a ternary operator instead of if-else.

if (myPath === '/') {
    next({ path: '/articles', query: { page: 1 } });
} else {
    next();
}

What I want to do is to replace all of that with just one line:-

next(myPath === '/' ? { path: '/articles', query: { page: 1 } } : nothing);

If I'm going in the right direction then it just mainly boils down to passing 'nothing' to the next function. This is a code sample from the VueJS router navigation guard by the way if it's any help.

Is something like this possible?

Aucun commentaire:

Enregistrer un commentaire