How to link the Id’s of two API’s to ANGULAR?

Asked

Viewed 34 times

1

Guys, I have a problem, and I’m kind of a layman with Angular.. I have 2 functions that consult two methods of a API.. need to compare the id's of the results and demonstrate only those that the id is equal, I thank you from now! I have the following code:

function SearchDespesa(response){
  $this.Despesa = response;
  $this.Despesa.DespesaId; // Aqui retorna um json com as informações..

  // Daqui em diante não sei oque fazer
}

function SearchTipoDespesa(response){
  $this.TipoDespesa = response;
  $this.Despesa.EmpresaId; // Aqui retorna um json com as informações..

  // Daqui em diante não sei oque fazer
}

1 answer

3


Just go through one of the two arrays, and make the comparison, I will demonstrate in javascript but in angular you will only add the $scope

var dados = [
  {id: 1, nome: 'teste'},
  {id: 2, nome: 'teste2'},
];

var tipos = [
	{id: 1, tipo: 'carro'},
 	{id: 2, tipo: 'musica'},
	{id: 3, tipo: 'bola'},
];

var resul = [];

for(var i = 0; i < dados.length; i++) {
   if(tipos[i].id === dados[i].id) {
     resul.push(Object.assign(dados[i],tipos[i]));
   }
}

console.log(resul);

  • 1

    Vlw brother, your idea helped me implement the script, thank you!

Browser other questions tagged

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