简体   繁体   中英

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 = ['sAdam1@green.com','lessio@gmail.com'];

email: sAdam1@green.com 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: ["mail@example.com","mail2@example.com",...]

Raw data: emails: john.doe@example.com, doe@hotmail.com, j.doe@gmail.com, dennis@gmail.com, cow@boy.com firstname: john, dennis, alice lastname: doe

and result mail array: cow@boy.com. rest to the trash.

Looking forward to the help. Thanks in advance

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

 var emails = ['john.doe@example.com', 'doe@hotmail.com', 'j.doe@gmail.com', 'dennis@gmail.com', 'cow@boy.com'], firstnames = ['john', 'dennis', 'alice'], lastnames = ['doe'], names = [...firstnames, ...lastnames], filtered = emails.filter(e => !names.some(n => e.includes(n))); console.log(filtered); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM