Return to Snippet

Revision: 7285
at July 17, 2008 06:33 by scarfboy


Updated Code
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;
}

Revision: 7284
at July 17, 2008 06:31 by scarfboy


Initial Code
//Unique-ify list. (set-in-list)
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; //the 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;
}

Initial URL

                                

Initial Description
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.

Initial Title
Javascript  set(-of-string) functions

Initial Tags
javascript

Initial Language
JavaScript