How to verify if a matrix contains a specific value?

Asked

Viewed 52 times

-1

I have the following function:

const role = tokenPayload.params.role.map(r => {
  return r.value;
}) [0];

She returns this:

author

If I remove index [0], returns this:

["author", "admin"]

Is it possible for the function to return all values in the same format as the first example? The "role" const will be used in a comparison === that accepts only the result in this specific format.

I will put the full function for better understanding:

canActivateChild(route: ActivatedRouteSnapshot): boolean {
    const helper = new JwtHelperService();
    const expectedRole = route.data.expectedRole;
    const token = this.authService.getToken();
    const tokenPayload = helper.decodeToken(token);
    const role: string = tokenPayload.params.role.map(r => {
  return r.value;
}) [0];
console.log(role);
if (!this.authService.isAuthenticated() || role !== expectedRole) {
  this.router.navigate(['/admin']);
  return false;
}
return true;
}

router Component:

{
    path: 'container-users',
    component: ContainerUsersComponent,
    canActivateChild: [AuthGuard],
    data: {
      expectedRole: 'admin'
    },
    children: [
      { path: '', component: ListUsersComponent },
      { path: 'list-users', component: ListUsersComponent },
      { path: 'form-new-user', component: FormNewUserComponent }
    ]
  }

At the moment, I am only passing one function per user. However, I would like to leave the dynamic code, in case any user has more than one function in the future.

1 answer

0

Take off the [0].

Thus:

const roles = tokenPayload.params.role.map(r => {
  return r.value;
});

console.log(roles);

The [0] is making you take the first value of the array generated by map.

Look at my example:

const usuarios = [{nome: "Wallace"}, {nome: "Wayne"}]

console.log(usuarios.map( function (u) {
   return u.nome; 
}));

console.log(usuarios.map( function (u) {
   return u.nome; 
})[0]);

  • I need the values to match the first print, that is, outside the array. Is it possible? Type [Author, admin] and not ["Author","admin"].

  • Thus? var author = roles[0]; var admin= roles[1];

  • the value that this function returns will be compared using "===" with a string. I’ll put the function that uses the role value: if (!this.authService.isAuthenticated() || role !== expectedRole) { this.router.navigate(['/admin']); Return false; } only works if you put [0] in the first function, with the answer returning in the way of the first print, which is the role. Only it can happen that the user has more than one function, if you put the [0], only returns the 1st function. I need you to return them all, but the way it looks in the first print. .

  • You did not put any of this information in your question. It was essential that you did so before the answer. Please edit your question and put all the information you want.

  • I explained in the question what I need, which is that the returned value comes in the same format as the first print. Sorry, but I believe my question is complete, because I really just need to know how to return the value in that format. I’d appreciate it if you could help me, but if you can’t, I’d appreciate it anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.