Difference between two arrays with TypeScript or ES6

0

How can I get the difference between two Arrays of objects using TypeScript or ES6 ?

I tried using SET :

   let arr1 = new Set(lista1);
   let arr2 = new Set(lista2);
   let difference = new Set(
       [arr1 ].filter(x => !arr2.has(x)));

But it did not work, this is the result I get:

That way the values are not listed, it might not be the most appropriate way to do what I want.

    
asked by anonymous 29.07.2016 / 21:13

1 answer

1

I solved the problem as follows:

let mapChamada = chamada.map(item => item.Matricula);
let mapPessoa = pessoas.map(item => item.Matricula);

let diff = mapChamada.map((Matricula, index) => {
  if (mapPessoa.indexOf(Matricula) < 0) {
    return chamada[index];
  }
}).concat(mapPessoa.map((Matricula, index) => {
  if (mapChamada.indexOf(Matricula) < 0) {
    return pessoas[index];
  }
})).filter(item => item != undefined);

console.log(diff);

My two arrays are these:

let chamada= [{
      Matricula: 434,
      Nome: 'Diego Augusto',
      PessoaId: 'bc61b0a1-2b8e-4c93-a175-21949ff2b240'
}];

let pessoas = [{
     Matricula: 434,
     Nome: 'Diego Augusto',
     PessoaId: 'bc61b0a1-2b8e-4c93-a175-21949ff2b240'
  }, {
     Matricula: 431,
     Nome: 'Crislayne',
     PessoaId: '11576497-7632-4c1b-9806-fed24b7608c2'
  }];

The result of the difference was:

diff [{
     Matricula: 431,
     Nome: 'Crislayne',
     PessoaId: '11576497-7632-4c1b-9806-fed24b7608c2'
  }];
    
01.08.2016 / 16:39