JavaScript除去数组中重复
这里我给出一个方法哈, Array.prototype.uniqueEx=function(){
var l=this.length;
var arr=[];
var tmp={}
for(var i=0;il;i++){
if(!tmp[this[i]]){
arr.push(this[i]);
tmp[this[i]]=1;
}
}
return arr;
} 和上面那个朋友给出打答案比较起来 ,代码似乎更多一点, 但是效率更高 我做了下测试 ,同样大小的数组,它的代码执行起来,需要耗费5毫秒, 而我的代码间隔是0.几毫秒。 如果数组更大,他的那种方法,效率明显要低很多 。 效率测试代码如下: script type=”text/javascript”Array.prototype.unique=function(filo) {
with(this) return filo ?
join(“,”).match( /([^,]+)(?!.*\1)/ig ) :
reverse().join(“,”).match( /([^,]+)(?!.*\1)/ig ).reverse();
}
Array.prototype.uniqueEx=function(){
var l=this.length;
var arr=[];
var tmp={}
for(var i=0;il;i++){
if(!tmp[this[i]]){
arr.push(this[i]);
tmp[this[i]]=1;
}
}
return arr;
}var testarr=[1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1,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 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5,1, 1, 1, 1, 2, 3, 4, 1, 2, 5];var timeold=new Date().getTime();
testarr=testarr.unique(); //这个是他的
var timenew=new Date().getTime();
alert(timenew-timeold);
alert(testarr); timeold=new Date().getTime();
testarr2=testarr2.uniqueEx(); //这个是我的
timenew=new Date().getTime();
alert(timenew-timeold);
alert(testarr2);/*对比一下2种方法的运算时间,就知道了*//script
js中怎么把数组中重复的数据取出来
需要准备的材料分别是:电脑、html编辑器、浏览器。
1、首先,打开html编辑器,新建html文件,例如:index.html。
2、在index.html的script标签中,输入js代码:
var a = [1, 6, 8, 5, 23, 6, 6, 7, 8], b = [], c = [];
for (var i = 0; i a.length; i++) {
if (c.indexOf(a[i]) === -1) {
c.push(a[i]);
} else {
b.push(a[i]);
}
}
document.body.innerText = b;
3、浏览器运行index.html页面,此时会打印出数组a中重复的数据。
mongo中的高级查询之聚合操作(distinct,count,group)与数据去重
Mongodb中自带的基本聚合函数有三种:count、distinct和group。下面我们分别来讲述一下这三个基本聚合函数。
(1)count
作用:简单统计集合中符合某种条件的文档数量。
使用方式:db.collection.count(query)或者db.collection.find(query).count()
参数说明:其中query是用于查询的目标条件。如果出了想限定查出来的最大文档数,或者想统计后跳过指定条数的文档,则还需要借助于limit,skip。
举例:
db.collection.find(query).limit();
db.collection.find(query).skip();
(2)distinct
作用:用于对集合中的文档针进行去重处理
使用方式:db,collection.distinct(field,query)
参数说明:field是去重字段,可以是单个的字段名,也可以是嵌套的字段名;query是查询条件,可以为空;
举例:
db.collection.distinct(“user”,{“age”:{$gt:28}});//用于查询年龄age大于28岁的不同用户名
除了上面的用法外,还可以使用下面的另外一种方法:
db.runCommand({“distinct”:”collectionname”,”key”:”distinctfied”,”query”:query})
collectionname:去重统计的集合名,distinctfield:去重字段,,query是可选的限制条件;
举例:
这两种方式的区别:第一种方法是对第二种方法的封装,第一种只返回去重统计后的字段值集合,但第二种方式既返回字段值集合也返回统计时的细节信息。
(3)group
作用:用于提供比count、distinct更丰富的统计需求,可以使用js函数控制统计逻辑
使用方式:db.collection.group(key,reduce,initial[,keyf][,cond][,finalize])
备注说明:在2.2版本之前,group操作最多只能返回10000条分组记录,但是从2.2版本之后到2.4版本,mongodb做了优化,能够支持返回20000条分组记录返回,如果分组记录的条数大于20000条,那么可能你就需要其他方式进行统计了,比如聚合管道或者MapReduce;
===========================================================
mongo中的高级查询之聚合操作(distinct,count,group)
1.distinct的实现:
2.count的实现
3.group的实现
(1).分组求和:类似于mysql中的 select act,sum(count) from consumerecords group by act
(2).分组求和,过滤。类似mysql中的select act,sum(count) from consumerecords group by act having act=’charge’;
(3).将时间格式化并且按时间分组求count,不推荐使用这种方法。
db.playerlogs.aggregate({ concat:[{ year:” substr:[{ start”},0,4]},{ dayOfMonth:” group:{_id:” sum:1}}},{$sort:{con:1}}) ;
group按时间分组(时间格式化)
javascriptz中时间的相关函数参考:
(4).group 分组方法实现的讲解。
group 的完整语法是。
参数解释:
key:需要分组的键或是函数(function),group分组实例3中的key就是一个函数值
initial:声明并且初始化变量。每一组共享一个变量值。多个变量之间用逗号隔开
$reduce:循环体,集合中有多少个文档,就会循环多少次。函数(function)中变量doc表示当前文档对象,
prev表示累积处理的结果对象(这个地方可能描述的不是很情况,自己慢慢体会)
finalize:可选参数,可以简单理解为对分组之后的结果的再次处理,doc表示group之后的文档对象(这一步也是一个循环体
condition:可选参数,对已经分组好的结果进行过滤,有点类似于mysql中的having
4.mapReduce:暂缺,要想玩转这个方法需要有很强的JavaScript功能。
据说mapReduce 可以实现很复杂的查询,可以将一个复杂的查询分拆到多个机器上运行,
然后把各个结果集组合起来,形成最终结果。但是很慢。
mapreduce用法大全
mapreduce性能调优
数组中去除重复值示例
5.db.runCommand的相关命令.
db.runCommand({distinct:”consumerecords”,key:”userId”}):键值去重 类似于mysql中的 select distinct userId from consumerecords
db.runCommand({distinct:”consumerecords”,key:”userId”}).values.length:去重之后求记录数,类似于mysql中的 select count(distinct userId) from consumerecords
db.runCommand({distinct:”consumerecords”,key:”userId”,query:{act:”charge”}}):去重之后求记录数,类似于mysql中的 select distinct userId from consumerecords where act=”charge”
db.runCommand(
… {
… group:
… {
… ns:”test2″, # 集合名
… key:{iname:true}, # 分组字段
… initial:{dd:0}, # 按照来初始化该值
… reduce之后 ,function返回的值
… {
… prev.dd=doc.iage+prev.dd #
… }
…
… }
… }
… )
数值以字符串形式存储的解决方案:
db.runCommand(
{
group:
{
ns:”consumerecords”,
key:{act:true},
initial:{ct:100,tt:0},
$reduce:function(doc,prev)
{
},
}
}
)
去重
1, 直接使用distinct 语句查询, 这种查询会将所有查询出来的数据返回给用户, 然后对查询出来的结果集求总数(耗内存,耗时一些)
var len = db.student.distinct(“name”,{“age” : 18}).length;
print(len)
注,使用这种方法查询时,查询的结果集大于16M 时会查询失败,失败信息如下:
{“message” : “distinct failed: MongoError: distinct too big, 16mb cap”,”stack” : “script:1:20”}
2, 使用聚合函数,多次分组统计结果,最终将聚合的结果数返回给用户
db.student.aggregate([
{ project:{“name”:true}},
{ name”}},
{ sum:1}}}
])
注,这种查询数据量大时就不会出现如上查询失败的情况,而且这种查询不管是内存消耗还是时间消耗都优于上面一种查询
ps:根据id分组,id指定为组合项的话,因为id不会重复,所以作用相当于把组合项去重了。
假如现在需要拿出collection的其他field的话,可以使用$push关键字
// 根据name和sex分组
// 把分组后的name,sex,age放到对应的Document下,形成一个数组
db.student.aggregate(
[
{
name”, sex: ” push: ” push: ” push: “$age”}
}
}
]
). forEach(function(x){
db.temp.insert(
{
name: x.name,
sex : x.sex,
age: x.age
}
);
});
用forEach、indexOf、push实现数组去重
方法一:去重复数据
script
Array.prototype.distinct=function(){
var a=[],b=[];
for(var prop in this){
var d = this[prop];
if (d===a[prop]) continue; //防止循环到prototype
if (b[d]!=1){
a.push(d);
b[d]=1;
}
}
return a;
}
var x=[‘a’,’b’,’c’,’d’,’b’,’a’,’e’,’a’,’b’,’c’,’d’,’b’,’a’,’e’];
document.write(‘原始数组:’+x);
document.write(“br /”);
document.write(‘去重复后:’+x.distinct());
/script
方法二:取重复数据
script type=”text/javascript”
Array.prototype.distinct=function(){
var a=[],b=[],c=[],d=[];
for(var prop in this){
var d = this[prop];
if (d===a[prop])
{
continue;
}//防止循环到prototype
if (b[d]!=1){
a.push(d);
b[d]=1;
}
else {
c.push(d);
d[d]=1;
}
}
//return a;
return c.distinct1();
}
Array.prototype.distinct1=function(){
var a=[],b=[];
for(var prop in this){
var d = this[prop];
if (d===a[prop]) continue; //防止循环到prototype
if (b[d]!=1){
a.push(d);
b[d]=1;
}
}
return a;
}
javascript里面的字面量是什么意思啊?
字面量表示如何表达这个值,一般除去表达式,给变量赋值时,等号右边都可以认为是字面量。
字面量分为字符串字面量(string literal )、数组字面量(array literal)和对象字面量(object literal),另外还有函数字面量(function literal)。
示例:
var test=”hello world!”;
“hello world!”就是字符串字面量,test是变量名。
javascript总for of和for in的区别
The for…of statement creates a loop iterating over iterable objects (including Array, Map, Set,String, TypedArray, arguments object and so on), invoking a custom iteration hook with statements to be executed for the value of each distinct property.
The for…in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.
———-from MDN
可以看出for…in遍历的是Object(包括Array)中的可枚举的属性,但是顺序是随机的,随着引擎不同而不同
但是for…of是对可遍历对象创建了一个循环,对对象里面每个元素调用一个钩子函数(个人理解就是可遍历对象要求实现的next()函数),输出结果