Posted By

jatkins on 01/25/11


Tagged

array unique


Versions (?)

array_unique


 / Published in: JavaScript
 

Released into the public domain. array_to_modify is required; remove_empty_items is an optional boolean argument. find_index is required because some versions of IE don't feature Array.indexOf.

  1. function find_index(array, string) {
  2. var i = 0;
  3. for(i=0;i<array.length;i++) {
  4. if(array[i]==string) break;
  5. }
  6. return array[i] == string ? i : -1;
  7. }
  8.  
  9. function array_unique(array_to_modify, remove_empty_items) {
  10. new_array = [];
  11. for(var i=0;i<array_to_modify.length;i++) {
  12. if(find_index(new_array, array_to_modify[i])==-1&&((remove_empty_items&&remove_empty_items==true&&array_to_modify[i].toString().replace(/\s/g, '')!='')||!remove_empty_items))
  13. new_array[new_array.length] = array_to_modify[i];
  14. }
  15. return new_array;
  16. }

Report this snippet  

You need to login to post a comment.