简体   繁体   English

JavaScript从数组过滤数据

[英]JavaScript filter data from array

I have 3 arrays: firstname, lastname, email: 我有3个数组:名字,姓氏,电子邮件:

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. 电子邮件:sAdam1@green.com被删除。 Good result, but when I use: var emails = email.map(a => a.email); 很好的结果,但是当我使用时: 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",...] 给出的结果类似于字符串数组:[“ 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 原始数据:电子邮件:john.doe @ example.com,doe @ hotmail.com,j.doe @ gmail.com,dennis @ gmail.com,cow @ boy.com名:john,dennis,alice姓:doe

and result mail array: cow@boy.com. 结果邮件数组: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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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