日期:2014-05-16 浏览次数:20448 次
unique: function( array ) { var ret = [], done = {}; try { for ( var i = 0, length = array.length; i < length; i++ ) { var id = jQuery.data( array[ i ] ); if ( !done[ id ] ) { done[ id ] = true; ret.push( array[ i ] ); } } } catch( e ) { ret = array; } return ret; }
Array.prototype.unquie=function(){ var newArray=new Array(); var len=this.length; for (var i=0;i<len ;i++){ for(var j=i+1;j<len;j++){ if(this[i]===this[j]){ j=++i; } } newArray.push(this[i]); } return newArray; }
Array.prototype.unquie=function(){ //需要考虑数组内容中包含boolean,string类型数据。 var newArray=[] , provisionalTable = {}; for (var i = 0, item; (item= this[i]) != null; i++) { if (!provisionalTable[item]) { newArray.push(item); provisionalTable[item] = true; } } return newArray; }
console.time('t'); var array = [1,false,'',true,true,4,4,4,4,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4]; console.info(array.unique()) console.timeEnd('t')