Repetition structure that compares equal values and printe the correspondence

-3

I have two arrays:

operator.permanent tab has screen ids and the screens array has id,

I need to create a repeat structure that compares the two arrays and printe the screen name.

I tried something like:

My array operator.table_perm:

(4) [1, 3, 9, 5]
0: 1
1: 3
2: 9
3: 5
length: 4

My screens array:

0: {id: 1, nome_tela: "Cadastrar Operador", created_at: null}
1: {id: 2, nome_tela: "Tipo do Produto", created_at: null}
2: {id: 3, nome_tela: "Produtos", created_at: null}
3: {id: 4, nome_tela: "Custos Fixos", created_at: null}
4: {id: 5, nome_tela: "Custos Variáveis", created_at: null}
5: {id: 6, nome_tela: "Custos Extras", created_at: null}
6: {id: 7, nome_tela: "Listagem dos Custos Fixos", created_at: null}

The operator.table_perm array has the IDS of the screens as values. The screen array has id and name, I need to name the screens that exist in the table_perm.

    for(let i=0; i<this.operador.tabela_perm.length; i++){
      for(let j=0;j<this.operador.tabela_perm.length; j++){
        if(this.operador.tabela_perm[i] == this.telas[j].id){
          console.log(this.telas[j].nome_tela)
        }
      }
    }
  }
    
asked by anonymous 10.10.2018 / 18:31

2 answers

1
What is happening is that in its second for , the stop condition is this.operador.tabela_perm.length , which in the example example is 4, then its if will only compare to position 4 of the screen array, its code should look like this:

for(let i = 0; i < this.operador.tabela_perm.length; i++) {
    for(let j = 0; j < this.telas.length; j++) {
        if(this.operador.tabela_perm[i] == this.telas[j].id) {
            console.log(this.telas[j].nome_tela)
        }
    }
}
    
11.10.2018 / 14:15
0

Since you are using Typescript, it would be nice to use its features, such as Arrow Functions , there is an example of how the same solution would look.

tabela_perm.forEach((perm)=>{
  console.log(telas.find((telaName)=> telaName.id == perm).nome_tela)
})
    
19.10.2018 / 16:40