/ Published in: JavaScript

Exploits hash keys uniqueness, but in doing so effectively toString()s everything, meaning this should not be used for much beyond strings and perhaps integers.
I used this when I wanted to do some client-side filtering of (integer) identifiers.
I used this when I wanted to do some client-side filtering of (integer) identifiers.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function unique(l) { var o=new Object(),ret=[]; for(i in l){o[l[i]]=1} for(k in o){ret.push(k)} return ret; } function union(l1,l2) { var o=new Object(),ret=[]; for(i in l1){o[l1[i]]=1} for(i in l2){o[l2[i]]=1} for(k in o){ret.push(k)} return ret; } function intersect(l1,l2) { var o=new Object(), ret=[]; for (i in l1) o[l1[i]]=1; //this value is ignored for (i in l2) if (o[l2[i]]!==undefined) ret.push(l2[i]); return ret; } function except(l1,l2) { var o=new Object(), ret=[]; for (i in l1) o[l1[i]]=1; for (i in l2) delete(o[l2[i]]); for (i in o) ret.push(i); return ret; }
Comments
