Get surplus positions from an array after comparing with another array?

1

I have a function (in JavaScript) that compares 2 arrays:

let diferencas = [];
    let numD = 0;
    for (let a = 0; a < dados.length; a++) {
        if (dados[a].idEmail === results[a].idEmail) {
            console.log('Repetido!')
        } else {
            numD++;
            diferencas.push(dados[a]);
        }
    }
    console.log('Diferenças encontradas: ' + numD)
    console.log(diferencas)

The 2 arrays are composed in their first 8 positions by identical objects, and the results array has only 8 positions since it comes from a MongoDB query and the dados array has 9 positions, since it comes from a query to the email provider and comes with more recent emails.

My problem is: How do I get those surplus positions in the array dados ??     

asked by anonymous 06.09.2017 / 21:42

2 answers

2

A solution to the problem itself is using filter , like this:

let diferencas = dados.filter(x => results.indexOf(x) ==-1 ? x : false );

Where filter starts from the dados array which is the largest, and filters by checking whether each element exists in results through indexOf . If the current element of dados does not exist in results it is returned x and so it appears in diferencas . Otherwise returns false and excludes diferencas

Example:

const dados = ['[email protected]','[email protected]','[email protected]','[email protected]', '[email protected]'];
const results = ['[email protected]','[email protected]','[email protected]','[email protected]'];

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 array of objects you can still use this solution but you have to apply an additional step to get only the emails, which is to first map the array to an array of strings with the emails, using map e then perform the same procedure:

const dados = [
{email : '[email protected]'},
{email : '[email protected]'},
{email : '[email protected]'},
{email : '[email protected]'},
{email : '[email protected]'}
];

const results = [
{email : '[email protected]'},
{email : '[email protected]'},
{email : '[email protected]'},
{email : '[email protected]'}
];

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 are not equal.

Example:

const dados = ['[email protected]','[email protected]', '[email protected]','[email protected]','[email protected]']; //desordenado
const results = ['[email protected]','[email protected]','[email protected]','[email protected]',]; //desordenado

let diferencas = dados.filter(x => results.indexOf(x) == -1 ? x:false);

console.log('Diferenças encontradas: ' + diferencas.length);
console.log(diferencas);
    
06.09.2017 / 23:09
1

Just check if results exists, if there is dados exceeded ...

var dados = [
{idEmail : '1'},
{idEmail : '2'},
{idEmail : '3'},
{idEmail : '4'},
{idEmail : '5'},
];

var results = [
{idEmail : '1'},
{idEmail : '2'},
{idEmail : '3'},
{idEmail : '4'},
];

let diferencas = [];
    let numD = 0;
    for (let a = 0; a < dados.length; a++) {
        if (results[a]){
        	if(dados[a].idEmail === results[a].idEmail) {
            console.log('Repetido!')
        }}else {
            numD++;
            diferencas.push(dados[a]);
        }
    }
    console.log('Diferenças encontradas: ' + numD);
    console.log(diferencas);
    
06.09.2017 / 22:27