Posted By

elightbo on 01/26/07


Tagged

javascript array


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

mozami
fabienthomas


Javascript detect and remove items in array (string or number)


 / Published in: JavaScript
 

  1. function detectItem(originalArray, itemToDetect) {
  2. var j = 0;
  3. while (j < originalArray.length) {
  4. if (originalArray[j] == itemToDetect) {
  5. return true;
  6. } else { j++; }
  7. }
  8. return false;
  9. }
  10.  
  11. //remove item (string or number) from an array
  12. function removeItem(originalArray, itemToRemove) {
  13. var j = 0;
  14. while (j < originalArray.length) {
  15. // alert(originalArray[j]);
  16. if (originalArray[j] == itemToRemove) {
  17. originalArray.splice(j, 1);
  18. } else { j++; }
  19. }
  20. // assert('hi');
  21. return originalArray;
  22. }

Report this snippet  

You need to login to post a comment.