返回列表

删除重复数组元素[对象]

默认分类 2011-08-24 00:36:53

1.Delete array unit. From John Resig http://ejohn.org/blog/javascript-array-remove/ function remove(array, from, to) { var rest = array.slice((to || from) + 1 || array.length); array.length = from < 0 ? array.length + from : from; return array.push.apply(array, rest); };

2.Delete duplicate array unit. function removeDuplicate(arr,subKey){ var l=arr.length,a=arr.concat(); var i,j,k,notExist; arr.length=0; if(subKey!==undefined) arr.push(subKey); for(i=0;i<l;i++){ notExist = true; for(j=arr.length-1;j>=0;j--){ if(arr[j] == a[i]) notExist= false; } if(notExist) arr.push(a[i]); } if(subKey!==undefined) arr.shift(); return arr; }

function b(){ var b = {'id':1}; var a = ['1',b,2,'2',null,0,5]; removeDuplicate(a); alert(a); } b();