JS數(shù)組去重與取重的示例代碼
更新時間:2014年01月24日 09:31:35 作者:
本篇文章主要是對JS數(shù)組去重與取重的示例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
方法一:去重復數(shù)據(jù)
<script>
Array.prototype.distinct=function(){
var a=[],b=[];
for(var prop in this){
var d = this[prop];
if (d===a[prop]) continue; //防止循環(huán)到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('原始數(shù)組:'+x);
document.write("<br />");
document.write('去重復后:'+x.distinct());
</script>
方法二:取重復數(shù)據(jù)
<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;
}//防止循環(huán)到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; //防止循環(huán)到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','f','f','g'];
document.write('原始數(shù)組:'+x);
document.write("<br />");
document.write('去重復后:'+x.distinct());
</script>
復制代碼 代碼如下:
<script>
Array.prototype.distinct=function(){
var a=[],b=[];
for(var prop in this){
var d = this[prop];
if (d===a[prop]) continue; //防止循環(huán)到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('原始數(shù)組:'+x);
document.write("<br />");
document.write('去重復后:'+x.distinct());
</script>
方法二:取重復數(shù)據(jù)
復制代碼 代碼如下:
<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;
}//防止循環(huán)到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; //防止循環(huán)到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','f','f','g'];
document.write('原始數(shù)組:'+x);
document.write("<br />");
document.write('去重復后:'+x.distinct());
</script>
您可能感興趣的文章:
- js數(shù)組去重的常用方法總結
- JS實現(xiàn)數(shù)組去重方法總結(六種方法)
- JavaScript數(shù)組去重的兩種方法推薦
- js實現(xiàn)數(shù)組去重、判斷數(shù)組以及對象中的內容是否相同
- js算法中的排序、數(shù)組去重詳細概述
- javascript數(shù)組去重3種方法的性能測試與比較
- javascript數(shù)字數(shù)組去重復項的實現(xiàn)代碼
- js indexOf()定義和用法
- JavaScript Array對象擴展indexOf()方法
- JavaScript從數(shù)組的indexOf()深入之Object的Property機制
- JavaScript使用indexOf()實現(xiàn)數(shù)組去重的方法分析
相關文章
使用p5.js實現(xiàn)動態(tài)GIF圖片臨摹重現(xiàn)
這篇文章主要為大家詳細介紹了使用p5.js實現(xiàn)動態(tài)GIF圖片臨摹重現(xiàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10js解析xml字符串和xml文檔實現(xiàn)原理及代碼(針對ie與火狐)
分別針對ie和火狐分別作了對xml文檔和xml字符串的解析,考慮到了瀏覽器的兼容性,至于在ajax環(huán)境下解析xml,其實原理是一樣的,只不過放在了ajax里,還是要對返回的xml進行解析,感興趣的朋友可以了解下,或許對你學習js解析xml有所幫助2013-02-02JS函數(shù)的call和apply的實現(xiàn)方法區(qū)別分析
這篇文章主要為大家介紹了JS函數(shù)的call和apply的實現(xiàn)方法區(qū)別分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10JavaScript使用shift方法移除素組第一個元素實例分析
這篇文章主要介紹了JavaScript使用shift方法移除素組第一個元素的用法,實例分析了javascript中shift方法的使用技巧,需要的朋友可以參考下2015-04-04Bootstrap中的fileinput 多圖片上傳及編輯功能
這篇文章主要介紹了Bootstrap中的fileinput 多圖片上傳及編輯功能的實現(xiàn),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09