Compare 2 arrays and save the difference between them in the database?

0

I have 2 object arrays, one of them is the one composed of emails that comes from the provider through IMAP protocol and I convert to objects and then put them in an array all of them.

The other is an array of emails that I look for in MongoDB and have the same structure as the first email, for example:

{
    "_id" : ObjectId("59af03cb4a202223c4cf7bea"),
    "idEmail" : "DM5PR13MB10209695536A9A5B3119967CCD960@DM5PR13MB1020.namprd13.prod.outlook.com",
    "remetente" : "[email protected]",
    "destinatario" : "[email protected]",
    "assunto" : "Teste de gravação no banco",
    "texto" : "Email diferente para gravar no banco\n",
    "box" : "INBOX"

}

What I want to do is a function that compares the two arrays, do not delete anything from the DB, just save the differences (in this case the most recent emails coming from the provider).

But the two arrays are not the same size, the array that comes with the new emails will always be larger than the one already in the DB.

What I need (and I'm trying to do) is: Compare the two arrays and add only the differences in MongoDB.

I've already researched a lot and so far I have not found anything that solved my problem ...

    
asked by anonymous 05.09.2017 / 22:21

3 answers

1

In fact, Alexandre's response only returns what is in array 1 and not in array 2, when in fact the verification should be done in the two array, follow my solution:

function validarDiferenca() {
        var r1 = [2,4,6,8];
        var r2 = [3,4,5,7,9];

        var apenasNoR1 = r1.filter(function (element, index, array) {
            if(r2.indexOf(element) == -1)
                return element;
        });

        var apenasNoR2 = r2.filter(function (element, index, array) {
            if(r1.indexOf(element) == -1)
                return element;
        });

        var todasAsDiferencas = apenasNoR1.concat(apenasNoR2);

        alert(todasAsDiferencas);
    }

If you need the ordered array, apply a sort at the end with the sort logic. But I think that solves your problem.

    
05.09.2017 / 22:58
1

This function populates an array with the difference between 2 arrays:

function validarDiferenca()
    {
        var r1 = [2,4,6,8];
        var r2 = [3,4,5,7,9];       
        var r3 =[];
        r1.forEach(function (element, index, array) {
            if(r2.indexOf(element) == -1)
               r3.push(element);
        });

        alert(r3);
    }
    
05.09.2017 / 22:43
1

I believe that @ alexandre-caballoti's solutions and @carvbru's solutions can be used, depending on the requirement, for me it was not very clear which of the answers the question applies. Anyway, I would just like to add more current ways of doing this, which would be using the new javascript features, in this case FILTER and the INCLUDES .

I created a small demo script link

Code:

function pegarDiferenca() {
    let r1 = [2,4,6,8];
    let r2 = [3,4,5,7,9];       
    let r3 = r1.filter( a => !r2.includes( a ) );

    console.log( r3 );
}

pegarDiferenca();

I hope I have helped!

    
29.06.2018 / 17:04