SharePoint JavaScript Multi Select Choice Field – return nice format

Getting some event items from SharePoint in JavaScript? Have a multiselect choice field for caragories? Here's some handy JavaScript for cleaning out the rubbish ';#' and returning a nice comma delimited list

function cleanArray(actual){
  var newArray = new Array();
  for(var i = 0; i<actual.length; i++){
      if (actual[i]){
        newArray.push(actual[i]);
    }
  }
  return newArray;
}

function getEventCategory(inCategoriesString)
{
        var eventCateory = '';
        var eventCategories = inCategoriesString.split(';#');
        eventCategories = cleanArray(eventCategories);
        
        if (eventCategories.length == 1)
        {
        	eventCateory = eventCategories[0];
        }
        else
        {
        	for (i = 0; i < eventCategories.length; i++)
        	{
        		if (i == eventCategories.length - 1)
        		{
        			eventCateory += eventCategories[i];
        		}
        		else
        		{
        			eventCateory += eventCategories[i] + ', ';
        		}
        	}
        }

        return eventCateory;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *