Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MapReduce用于删除字符串的重复项_Javascript_Node.js_Mapreduce - Fatal编程技术网

Javascript MapReduce用于删除字符串的重复项

Javascript MapReduce用于删除字符串的重复项,javascript,node.js,mapreduce,Javascript,Node.js,Mapreduce,我有一个映射函数,它可以从email id中找出域名,并发出该函数以减少不计算域名的功能 [ { email:"xyz@gmail.com"}, { email:"abc@abc.com"}, { email:"inder@hotmail.com"}, { email:"Ravi@Hotmail.com"}, { email:"xxx@GMail.com"}, ] 下面是函数 db.collection.mapReduce( function()

我有一个映射函数,它可以从email id中找出域名,并发出该函数以减少不计算域名的功能

[
    { email:"xyz@gmail.com"},
    { email:"abc@abc.com"},
    { email:"inder@hotmail.com"},
    { email:"Ravi@Hotmail.com"},
    { email:"xxx@GMail.com"},
]
下面是函数

db.collection.mapReduce(
    function() {
        emit(this.email.substr(this.email.indexOf('@') + 1), 1);  
    }, 
    function(host, count) { 
        return Array.sum(count) ; }, 
    { out: "hosts" } 
)
输出良好:-

   gmail.com
   abc.com
   hotmail.com
   Hotmail.com
   GMail.com
但我想要的是

   gmail.com
   abc.com
   hotmail.com

我不想有重复的域名与大写字母之间和相同的名称之前。你知道如何删除大写字母的副本吗。或者任何相关的例子也是好的

在emit函数中,可以将其设置为返回域的小写字母,如:
this.email.substr(this.email.indexOf('@')+1.toLowerCase()


这样从所有的
GMail.com
GMail.com
GMail.com
等等,你只会得到
GMail.com
,使用ES5,你可以这样做:

var-arr,res=[];
arr=[
{电子邮件:xyz@gmail.com"},
{电子邮件:abc@abc.com"},
{电子邮件:inder@hotmail.com"},
{电子邮件:Ravi@Hotmail.com"},
{电子邮件:xxx@GMail.com"}
];
arr.map(功能(项目){
返回item.email.substr(item.email.indexOf('@')+1.toLowerCase();
}).forEach(功能(项目){
如果(资源索引(项目)=-1){
物品推送;
}
});

控制台日志(res)使用
Set
获得唯一值的一个线性:

const arr=[
{电子邮件:xyz@gmail.com"},
{电子邮件:abc@abc.com"},
{电子邮件:inder@hotmail.com"},
{电子邮件:Ravi@Hotmail.com"},
{电子邮件:xxx@GMail.com"},
];
const r=[…新集合(arr.map({email})=>email.toLowerCase().substr(email.indexOf('@')+1))]

console.log(r)
Change
this.email.substr(this.email.indexOf('@')+1)
this.email.substr(this.email.indexOf('@')+1).toLowerCase()
No这对我不起作用(this.username.substr((this.username.indexOf('@')+1.toLowerCase()),1);事实上,此更改后不会出现任何问题。尝试了这样的发射函数,但没有出现任何问题。emit(this.username.substr((this.username.indexOf('@')+1.toLowerCase()),1)。所以它不起作用。在作为答案发布之前,您是否尝试过这个mapreduce函数?您的代码运行良好,但您必须了解您在代码中所做的工作。任何有助于理解的评论都是好的