繁体   English   中英

JavaScript从数组过滤数据

[英]JavaScript filter data from array

我有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);

并且我正在尝试按名字过滤电子邮件数组(名字后面将如何排列):

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

当我使用示例时它工作:

var emails = ['sAdam1@green.com','lessio@gmail.com'];

电子邮件:sAdam1@green.com被删除。 很好的结果,但是当我使用时: var emails = email.map(a => a.email); 不起作用

console.log(emails); 给出的结果类似于字符串数组:[“ mail@example.com”,“ mail2@example.com”,...]

原始数据:电子邮件:john.doe @ example.com,doe @ hotmail.com,j.doe @ gmail.com,dennis @ gmail.com,cow @ boy.com名:john,dennis,alice姓:doe

结果邮件数组:cow@boy.com。 休息到垃圾桶。

期待得到帮助。 提前致谢

您可以通过检查不需要的名称数组来进行过滤。

 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