Giacomo
Giacomo

Reputation: 341

JavaScript filter data from array

I have 3 arrays: firstname, lastname, email:

var names = firstname.map(a => a.firstname);

    var uniqueNames = [];
    $.each(names, function(i, el){
        if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
    });

    var lastnames = lastname.map(a => a.lastname);


    var uniqueLastNames = [];
    $.each(lastnames, function(i, el){
        if($.inArray(el, uniqueLastNames) === -1) uniqueLastNames.push(el);
    });

    var emails = email.map(a => a.email);

and I'm trying to filter the array of emails by first names (how it will be followed by names):

var result = emails.filter(email => !uniqueNames.find(name => email.includes(name)));

it work when I use example:

var emails = ['[email protected]','[email protected]'];

email: [email protected] is deleted. Good result, but when I use: var emails = email.map(a => a.email); Doesn't work

console.log(emails); is giving result like a array of string: ["[email protected]","[email protected]",...]

Raw data: emails: [email protected], [email protected], [email protected], [email protected], [email protected] firstname: john, dennis, alice lastname: doe

and result mail array: [email protected]. rest to the trash.

Looking forward to the help. Thanks in advance

Upvotes: 1

Views: 436

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386848

You could filter by checking with an array of the unwanted names.

var emails = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]'],
    firstnames = ['john', 'dennis', 'alice'],
    lastnames = ['doe'],
    names = [...firstnames, ...lastnames],
    filtered = emails.filter(e => !names.some(n => e.includes(n)));
    
console.log(filtered);

Upvotes: 2

Related Questions