Tokenizing strings in javascript

satya - Sat May 05 2012 13:04:00 GMT-0400 (Eastern Daylight Time)

Read this article

Read this article

satya - Sat May 05 2012 13:04:54 GMT-0400 (Eastern Daylight Time)

javascript split function

javascript split function

Search for: javascript split function

satya - Sat May 05 2012 13:06:25 GMT-0400 (Eastern Daylight Time)

jquery foreach over an array

jquery foreach over an array

Search for: jquery foreach over an array

satya - Sat May 05 2012 13:08:01 GMT-0400 (Eastern Daylight Time)

here is an example of walking through an array

here is an example of walking through an array

satya - Sat May 05 2012 13:09:06 GMT-0400 (Eastern Daylight Time)

you can do this


for (i=0;i<array.length;i++)
{
  somefunction(array[i]);
}

function somefunction(x)
{
}

satya - Sat May 05 2012 13:10:49 GMT-0400 (Eastern Daylight Time)

or in jquery


$.each(array,somefunction);

functions somefunction()
{
   var x = this;
}

satya - Sat May 05 2012 13:34:41 GMT-0400 (Eastern Daylight Time)

here is an example


function showPost()
{
   var fieldSpec="{{f_field_names}}";
   var fieldList = fieldSpec.split("|");
   
   var fieldSpecPost = "";
   var first = true;
   for(i=0;i<fieldList.length;i++)
   {
       var indFieldSpec = fieldList[i];
       if (first == true)
       {
         first=false;
         fieldSpecPost += getSamplePostForFieldSpec(indFieldSpec);
      }
      else
      {
         fieldSpecPost += "&";
         fieldSpecPost += getSamplePostForFieldSpec(indFieldSpec);
      }
   }
   alert(fieldSpecPost);
}
function getSamplePostForFieldSpec(fieldSpec)
{
   var fieldSpecParts = fieldSpec.split(",");
   var fieldName = fieldSpecParts[0];
   var fieldType = fieldSpecParts[1];
   if (fieldType.indexOf("num") >= 0)
   {
      //it is a number
      return fieldName + "=10";
   }
   if (fieldType.indexOf("date") >= 0)
   {
      //it is a date
      return fieldName + "=04/15/2012";
   }
   return fieldName + "=" + fieldName + " value";
}