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

Asked

Viewed 8,565 times

2

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

The other is an array of emails that I search in Mongodb and have the same structure as the first email, follow 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, does not delete anything from DB, just save the differences (in case the latest emails coming from the provider).

However the two arrays do not have the same size, the array that comes with the new emails will always be bigger than the one that is already in DB.

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

Only I’ve done a lot of research and so far I haven’t found anything to solve my problem...

3 answers

4

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

I created a small demo script https://jsfiddle.net/a35p607e/

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’ve helped!

3


In fact, Alexandre’s answer only returns what there is in array 1 and not in array 2, when in fact the verification should be done in both array, follows 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 ordering logic. But I think this solves your problem.

  • I’ll try it out right here

  • I couldn’t understand where I could put this function in my code: https://codepen.io/LeonardoEbert/pen/mMYVjO?editors=1011

  • I implemented the code, but it saves the whole array, concatenates the 2 arrays and saves everything in the database, here is the complete code: https://jsfiddle.net/dxkyh3d8/1/

  • I don’t understand what the problem is now! :)

  • I’m still learning, and I kind of got a solution to compare the arrays: https://jsfiddle.net/pzmb75oy/ Since the 2 arrays are the same sequence, but the one coming from the provider has new objects at the end, then I got the ID’s of the respective positions of each array and now I need to capture those that are the new emails, thank you if you can take a look at that jsfiddle link...

2

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);
    }
  • I believe it will be of great help, thank you very much

  • If it helped, mark it as useful. ;)

  • Tomorrow I will have time to see and test the will, now I have no more time...

Browser other questions tagged

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