Take over positions of an array after comparison with another array?

Asked

Viewed 126 times

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, with the array results has only 8 positions as it comes from a query to Mongodb and the array dados has 9 positions as it comes from a query to the email provider and comes with newer emails.

My problem is: How Do I Pick Up Those Over Positions in the Array dados ??

2 answers

2


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 = ['[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 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 : '[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 is 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);

  • That example of object arrays also brings only the difference independent of the order of the objects??

  • @Leonardoebert Yes, I just exemplified with the string array to be simpler.

  • 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

  • Here is the entire code: https://jsfiddle.net/70ebprun/ ...

  • The mapping has to be adjusted to your example, of const dados2 = dados.map(x => x.email); for const dados2 = dados.map(x => x.idEmail);. Watch out for the difference between .email for .idEmail. I in response exemplified with field email to make it clearer to the reader

  • Kkkkkkk, I didn’t see this difference Now you found 3 differences... Thank you @Isac, you gave the perfect solution to my problem

  • I will only need now to adapt to save all the data of the objects in the database

  • @Leonardoebert I edited also for that detail. But would have to remap again with map to have the differences in object format let diferencasObj = diferencas.map(x => dados[dados2.indexOf(x)]);

  • That your latest edition worked perfectly too, now just save the different objects in the database

Show 4 more comments

1

Just make sure results exists if there is no 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);

  • Excellent, it worked 100%, thank you very much @Felipe Duarte, I was already in this problem 2 days ago... ; )

  • Normal kkk, javascript is right.

Browser other questions tagged

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