Compare two object arrays?

Asked

Viewed 578 times

0

I need to compare 2 arrays in Javascript, one that returns from a Mongodb query and the other comes from the email provider.

The two comparison criteria are the email ID and the box to which it belongs, and the email will be discarded if THE TWO criteria match, otherwise the email will be saved in the bank.

So far everything ok, the problem is that I do not know the logic nor the methods to do it correctly, I have tried using 2 for and none of it worked.

Server:

let dados = [];
    for (let l = 0; l < array.length; l++) { //array é o vetor com os emails que vem do provedor
        dados.push({
            idEmail: array[l].id,
            remetente: array[l].remetente,
            destinatario: array[l].destinatario,
            assunto: array[l].assunto,
            texto: array[l].texto,
            box: flag
        });
    }
    let pesquisa = {'idEmail': {$ne: dados.idEmail}, 'box': {$ne: dados.box}};
    db.open(function (err, mongoclient) {
        mongoclient.collection('emails', function (err, collection) {
            collection.find(pesquisa).toArray(function (err, results) {
                if (err){
                    console.log(err);
                    mongoclient.close()
                } else {
                let comp = [];
                for (let a = 0; a < results.length; a++) {
                    comp.push({
                        id: results[a].idEmail,
                        caixa: results[a].box
                    });
                }
                for (let l = 0; l < comp.length; l++) {
                    if (comp[l].idEmail === dados[l].idEmail && comp[l].caixa === dados.box) {
                        console.log('Repetido')
                    } else if (comp[l].idEmail !== dados[l].idEmail && comp[l].caixa !== dados.box) {
                        mongoclient.collection('emails', function (err, collection) {
                            collection.insert(dados, function (err, records) {
                                if (err) {
                                    console.log(err)
                                } else {
                                    console.log(records)
                                }
                                mongoclient.close()
                            })
                        })
                    }
                }
            }
        })
   })

1 answer

1


In

comp.push({
   id: results[a].idEmail,
   caixa: results[a].box
});

Your key is referred to as id and in condition is treated as value

sure would be

    if (comp[l].id === dados[l].idEmail && comp[l].caixa === dados.box) {
                            console.log('Repetido')
                        } else {
                        mongoclient.collection('emails', function (err, collection) {
                        collection.insert(dados, function (err, records) {
                            if (err) {
                                console.log(err)
                            } else {
                                console.log(records)
                            }
                            mongoclient.close()
                        })
                    })
    }

also note that your else if contrary to his if making it simply false, just replace it with else.

  • Okay, I’ll test here and I’ll get back to you with the result. if that works??

  • remove Else if and leave only Else and replace your if with the if above, I edited to make it easier

  • I implemented this code that you edited, but is not recording in the database...

  • And now I’ve discovered that the code isn’t even running, it stops before it reaches these functions...

  • always good to keep an eye on the console, and go debugging the values with 'console.log' if the arrays you want to compare are associative the above code is enough if you want to compare each value with an integer array, 2 loopings are needed or another example function array_map that does this mt well

  • 1

    That’s what I’ll do, thank you so much for helping @Felipe Duarte

Show 1 more comment

Browser other questions tagged

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