Compare two object arrays?

0

I need to compare 2 arrays in Javascript, one that returns from one 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 database.

So far so good, the problem is that I do not know the logic or the methods to do it correctly, I've already tried using 2 for and none 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()
                            })
                        })
                    }
                }
            }
        })
   })
    
asked by anonymous 05.09.2017 / 19:08

1 answer

1

In

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

Its key is referenced as id and in condition it is treated as value

The right thing 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 notice that your else if counter your if making it simply false, just replace it with else .

    
05.09.2017 / 19:21