A solution to the problem itself is by using the filter
, thus:
let diferencas = dados.filter(x => results.indexOf(x) ==-1 ? x : false );
In which the filter
part of the array dados
which is the largest, and filters to see if each element exists in the results
through the indexOf
. If the current element of dados
not exist in results
is returned x
and so appears in diferencas
. Otherwise it returns false
and excludes from diferencas
Example:
const dados = ['a@a.com','b@b.com','c@c.com','d@d.com', 'e@e.com'];
const results = ['a@a.com','b@b.com','c@c.com','d@d.com'];
let diferencas = dados.filter(x => results.indexOf(x) == -1 ? x:false);
console.log('Diferenças encontradas: ' + diferencas.length);
console.log(diferencas);
If you have an object array, you can still use this solution, but you have to take an additional step to stay with the emails, which is to first map the array to an array of strings with the emails, using the map
and then perform the same procedure:
const dados = [
{email : 'a@a.com'},
{email : 'b@b.com'},
{email : 'c@c.com'},
{email : 'd@d.com'},
{email : 'e@e.com'}
];
const results = [
{email : 'a@a.com'},
{email : 'b@b.com'},
{email : 'c@c.com'},
{email : 'd@d.com'}
];
const dados2 = dados.map(x => x.email); //mapeamento aqui
const results2 = results.map(x => x.email);//mapeamento aqui
//o resto igual
let diferencas = dados2.filter(x => results2.indexOf(x)==-1?x:false);
console.log('Diferenças encontradas: ' + diferencas.length);
console.log(diferencas);
//Para diferenças em formato de objeto pode-se utilizar o map do diferencas para dados
let diferencasObj = diferencas.map(x => dados[dados2.indexOf(x)]);
console.log(diferencasObj);
Comparing with the solution that has this has the advantage of also working even if the order of the arrays is not equal.
Example:
const dados = ['b@b.com','c@c.com', 'e@e.com','a@a.com','d@d.com']; //desordenado
const results = ['d@d.com','a@a.com','b@b.com','c@c.com',]; //desordenado
let diferencas = dados.filter(x => results.indexOf(x) == -1 ? x:false);
console.log('Diferenças encontradas: ' + diferencas.length);
console.log(diferencas);
That example of object arrays also brings only the difference independent of the order of the objects??
– LeonardoEbert
@Leonardoebert Yes, I just exemplified with the string array to be simpler.
– Isac
I’m doing some tests here, but he’s not identifying the differences... I’ll put all the code in a jsfiddle and I’ll send you the link
– LeonardoEbert
Here is the entire code: https://jsfiddle.net/70ebprun/ ...
– LeonardoEbert
The mapping has to be adjusted to your example, of
const dados2 = dados.map(x => x.email);
forconst dados2 = dados.map(x => x.idEmail);
. Watch out for the difference between.email
for.idEmail
. I in response exemplified with fieldemail
to make it clearer to the reader– Isac
Kkkkkkk, I didn’t see this difference Now you found 3 differences... Thank you @Isac, you gave the perfect solution to my problem
– LeonardoEbert
I will only need now to adapt to save all the data of the objects in the database
– LeonardoEbert
@Leonardoebert I edited also for that detail. But would have to remap again with
map
to have the differences in object formatlet diferencasObj = diferencas.map(x => dados[dados2.indexOf(x)]);
– Isac
That your latest edition worked perfectly too, now just save the different objects in the database
– LeonardoEbert