2

Thanks in advance to anyone who can think of a more efficient or a better way to do what my Javascript code below does:

var availableCharacters=Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");

for (counter=0; counter<availableCharacters.length; counter++){
  if(availableCharacters[counter]=="i"||
     availableCharacters[counter]=="l"||
     availableCharacters[counter]=="I"||
     availableCharacters[counter]=="L"||
     availableCharacters[counter]=="1"||
     availableCharacters[counter]=="0"||
     availableCharacters[counter]=="O"){
      availableCharacters.splice(counter, 1);
    }
}

What I'm trying to do is run through an array and remove any elements in that array that are "i", "l", "I", "L", "1", "0" or "O". Whilst this does work it seems like it might be slow and a bit cumbersome. If there is a better way? If not then not a problem, but most of the time when I do something that doesnt seem right to me, it's not! So I thought I'd ask S.O.

Thanks :)

  • possible duplicate of [Check variable equality against a list of values](http://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values) – pimvdb Jun 19 '12 at 21:21

5 Answers5

6

More recent browsers support Array.filter:

var availableCharacters = ........;
availableCharacters = availableCharacters.filter(function(a) {
  return !a.match(/[ilLI10O]/);
});

For older browsers, however, the for loop given by Mark Linus is good.

Alexander
  • 23,432
  • 11
  • 63
  • 73
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Thank you, I'm a fan of short and easy to read code. As it mentioned below that Regexes aren't taxing on modern day browsers, I think this fixes my problem very well. Can anyone see mobile Safari having a problem with looping though an 250-300 element array with this method? It's not critical but if this code would perform well on iOS devices aswell that would be a bonus! – GhostInTheSecureShell Jun 19 '12 at 21:33
4
var availableCharacters=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
for (counter=0; counter<availableCharacters.length; counter++){
    if(/[ilIL10O]/.test(availableCharacters[counter])){
        availableCharacters.splice(counter, 1);
    }
}
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67
1

Alternative solution with simple filter list:

var availableCharacters=Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3",     "4", "5", "6", "7", "8", "9");
var filter = ['i', 'l', 'I', 'L', '1', '0', 'O'];
for (counter=0; counter<availableCharacters.length; counter++){
    if(filter.indexOf(availableCharacters[counter]) >= 0) {
        availableCharacters.splice(counter, 1);
    }
}
jsalonen
  • 29,593
  • 15
  • 91
  • 109
0

If you're willing to use jQuery you can use grep() and write it like this:

Fiddle here

var availableChars = ["a", "b", "c", '1', 'i', 'o', "9"];
var result = $.grep(availableChars, function(c) {
    return !c.match(/[^il10o]/i)
})
document.write(result)​
Cuadue
  • 3,769
  • 3
  • 24
  • 38
  • 1
    This essentially returns an array of the characters in the regexp. I think you meant to negate. – pimvdb Jun 19 '12 at 21:31
0

filter is nice, but a simple string replace works without a net-

   var availableCharacters=["a", "b", "c", "d", "e", "f", "g", "h", "i", 
   "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", 
   "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];

  var allowed= availableCharacters.join('').replace(/[iLl10O]/g,'').split('');

  returned value: (Array)
  a,b,c,d,e,f,g,h,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,2,3,4,5,6,7,8,9
kennebec
  • 102,654
  • 32
  • 106
  • 127