Angular 7 - Compare Lists

Asked

Viewed 314 times

1

I have two lists in my component.

listPermissoesOfUser: UsuarioPermissoes[];
listAnotherPermissoes: UsuarioPermissoes[];

And when I upload the page, I display them in a primeNG picklist (Two containers). Only when I press, I need to buy the two lists, whatever you have on listPermissionsOfUser cannot appear in the listAnotherPermissions

The function that loads the page information is below:

loadPage() {
    this.idUser = this.route.snapshot.params.id;

    this.route.paramMap.pipe(
      switchMap(params => this.usuarioPermissoesService.getPermissoesOfUser(+params.get("id")))
    ).subscribe(
      (listPermissoesOfUser: UsuarioPermissoes[]) => {this.listPermissoesOfUser = listPermissoesOfUser; },
      (error) => {console.log(error); }
    );
    this.usuarioPermissoesService.getListFuncionalidades().subscribe(
      (listAnotherPermissoes: UsuarioPermissoes[]) => {this.listAnotherPermissoes = listAnotherPermissoes},
      (error) => {console.log(error); }
    );
    //Funcionalidades que o usuario tem, não aparece na listAnotherPermissoes

  }

Any idea how to do that?

  • Isn’t it better to already come ready in return, IE, where does this information come from? wouldn’t it be from there, I did formatted all the information.

  • You mean in the database? What would be the command for a SELECT do a search and do not return the lines in common with the "id_usuario"?

  • So your problem isn’t there and your question turns out to be another ...

  • I asked that nut I’ve tried to do this in the database, and the query does not work because of the few Fk’s in the tables. I’ve tried using LEFT JOIN, Where IS NOT, <> among others, ma snenhum meets what I need, so I find it easier to try doing it in Angular or maybe in PHP.

  • Look if this research is not working out if you should have problems in your back end and then it gets complicated to say anything.

  • I have two SELECT’s in DB, one of them searches the user permissions; the other searches all permissions. In the second SELECT, I would have to do a search that, because of the way the tables are turned on, would not have to do in a query, would have to make two separate queries and bring the result of them, but if I do this, I would have to do in PHP. Do in PHP or Angular would in the same.

  • If you want, I can post the tables so you can analyze the situation better.

  • It has to be in another question, because, would be two questions.

  • 1
Show 4 more comments

1 answer

1


You could do it like this:

var x = [1,2,3];
var y = [3,4,6];
x.filter(x => y.find(y => y == x)); // [3]

It will result in an array composed of duplicates, so you can remove these in the list of all permissions.

var x = [1,2,3];
var y = [3,4,6];
x.filter(x => y.indexOf(x) < 0); // [1,2]

Browser other questions tagged

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