JS實現(xiàn)的Object數(shù)組去重功能示例【數(shù)組成員為Object對象】
本文實例講述了JS實現(xiàn)的Object數(shù)組去重功能。分享給大家供大家參考,具體如下:
目標:實現(xiàn)成員為 Object 的數(shù)組的去重。
注意,這里的數(shù)組成員為 Object,而不是數(shù)值或者字符串。
調(diào)用方法:
arr = distinct_arr_element(arr);
函數(shù):
/* * 在數(shù)組中去除重復(fù)項() */ var distinct_arr_element = function( arr ){ if( !arr ) return null ; var resultArr = []; $(arr).each( function( index, el ){ var notExist = true ; $(resultArr).each( function(i,element){ if( isObjectValueEqual( el, element ) ){ notExist = false ; return false ; } }); if( notExist ) resultArr.push( el ); }); return resultArr ; } /* * 判斷兩個 Object 的值是否相等 */ function isObjectValueEqual(a, b) { // Of course, we can do it use for in Create arrays of property names var aProps = Object.getOwnPropertyNames(a); var bProps = Object.getOwnPropertyNames(b); // If number of properties is different, objects are not equivalent if (aProps.length != bProps.length) { return false; } for ( var i = 0; i < aProps.length; i++ ) { var propName = aProps[i]; // If values of same property are not equal, objects are not equivalent if (a[propName] !== b[propName]) { return false; } } // If we made it this far, objects are considered equivalent return true; }
完整測試示例如下:
<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script> <script> /* * 在數(shù)組中去除重復(fù)項() */ var distinct_arr_element = function( arr ){ if( !arr ) return null ; var resultArr = []; $(arr).each( function( index, el ){ var notExist = true ; $(resultArr).each( function(i,element){ if( isObjectValueEqual( el, element ) ){ notExist = false ; return false ; } }); if( notExist ) resultArr.push( el ); }); return resultArr ; } /* * 判斷兩個 Object 的值是否相等 */ function isObjectValueEqual(a, b) { // Of course, we can do it use for in Create arrays of property names var aProps = Object.getOwnPropertyNames(a); var bProps = Object.getOwnPropertyNames(b); // If number of properties is different, objects are not equivalent if (aProps.length != bProps.length) { return false; } for ( var i = 0; i < aProps.length; i++ ) { var propName = aProps[i]; // If values of same property are not equal, objects are not equivalent if (a[propName] !== b[propName]) { return false; } } // If we made it this far, objects are considered equivalent return true; } var arrDemo=[{'name':'jb51.net'},{'name':'jb51.net'},{'age':10},{'age':12}]; console.log(distinct_arr_element(arrDemo)) </script>
使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼,可得如下運行結(jié)果:
PS:這里再為大家提供幾款相關(guān)工具供大家參考使用:
在線去除重復(fù)項工具:
http://tools.jb51.net/code/quchong
在線文本去重復(fù)工具:
http://tools.jb51.net/aideddesign/txt_quchong
更多關(guān)于JavaScript相關(guān)內(nèi)容還可查看本站專題:《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript字符與字符串操作技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)學(xué)運算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
解決function函數(shù)內(nèi)的循環(huán)變量
鼠標放到指定的行上自動彈出當前的個數(shù),從0開始,這個功能方便我們在tab切換中定位2008-10-10JavaScript實現(xiàn)復(fù)制內(nèi)容到粘貼板代碼
最近做了一個前端項目,其中有需求:通過button直接把input或者textarea里的值復(fù)制到粘貼板里。下面小編給大家分享JavaScript實現(xiàn)復(fù)制內(nèi)容到粘貼板代碼,需要的朋友參考下2016-03-03JavaScript 函數(shù)式編程實踐(來自IBM)
說到函數(shù)式編程,人們的第一印象往往是其學(xué)院派,晦澀難懂,大概只有那些蓬頭散發(fā),不修邊幅,甚至有些神經(jīng)質(zhì)的大學(xué)教授們才會用的編程方式。2010-06-06如何使用JavaScript和XLSX.js將數(shù)據(jù)導(dǎo)出為Excel文件
這篇文章主要給大家介紹了關(guān)于如何使用JavaScript和XLSX.js將數(shù)據(jù)導(dǎo)出為Excel文件的相關(guān)資料,xlsx.js基于JavaScript的Excel文件讀寫庫 如果你需要在瀏覽器端處理Excel文件,那么xlsx.js可能是一個不錯的選擇,需要的朋友可以參考下2024-05-05