#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    44
    Rep Power
    14

    Question JavaScript Arrays


    Does anyone now if there is a function in java script that would tell me how many times a certain string appears in an array. IE

    Array[1] = 1
    Array[2] = 2
    Array[3] = 1

    So if I searched for 1 it would tell me it appeared twice.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    Dublin
    Posts
    413
    Rep Power
    14
    A bit crude and not tested - but you'll get the idea...

    Code:
    function countOccurences(array,item)
     {var j=0;
      for (var i=0;i<array.length;i++)
       {j+=array[i]==item?1:0;}
      return j;
     }

IMN logo majestic logo threadwatch logo seochat tools logo