decorative image for this page

Converting a collection to a JScript object in ASP

30th May 2006 13:04 | Categories: HowTo, ASP

Enumerators are collections of data that can be iterated through. Although useful, they are not compliant with ECMAScript standards, and therefore I prefer not to use them. Some objects in the ASP environment however, such as Request.Form, are provided as Enumerators, and need to be converted them to standard JScript objects. Here’s a couple of routines to do so.

I keep all the functions below in a generic include, coll2obj.asp, and use it across projects. This is a generic collection-to-object conversion, which doesn’t seem to work on some built-in collections.

/*    coll2obj
 * transforms a VB collection thing in a JS-friendly object
 *
 */
function coll2obj( coll )
{
    var enm    = new Enumerator( coll );
    var obj    = new Object();
    while( !enm.atEnd() )
    {
        obj[enm.item().name]    = enm.item().value;
        enm.moveNext();
    }
    return obj;
}

The function below converts the Request.ServerVariables collection into a simple JSCript file object.

/*    env2obj
 *    transforms Request.ServerVariables VB collection thing in a JS-friendly object
 *
 */
function env2obj()
{
	var enm    = new Enumerator(Request.ServerVariables);
	var obj    = new Object();
	var ei, en;
	while(!enm.atEnd())
	{
		ei    = enm.item();
		if( Request.ServerVariables.Item( ei ).Count > 1 )
		{
			obj[ei]    = new Array();
			en    = new Enumerator( Request.ServerVariables( ei  );
			while( !en.atEnd() )
			{
				obj[ei][ obj[ei].length ]    = en.item();
				en.moveNext();
			}
			en    = null;
		}
		else
		{
			obj[ei]    = new String( Request.ServerVariables( ei ) );
		}
		enm.moveNext();
	}
	return obj;
}

This converts post variables to a JScript object.

/*    post2obj
 *    transforms Request.Form VB collection thing in a JS-friendly object
 *
 */
function post2obj()
{
	var enm    = new Enumerator( Request.Form );
	var obj    = new Object();
	var ei, en;
	while( !enm.atEnd() )
	{
		ei    = enm.item();
		if(  Request.Form.Item( ei ).Count > 1 )
		{
			obj[ei]    = new Array();
			en    = new Enumerator(Request.Form( ei ));
			while( !en.atEnd() )
			{
				obj[ei][ obj[ei].length ]    = en.item();
				en.moveNext();
			}
			en    = null;
		}
		else
		{
			obj[ei]    = new String(Request.Form( ei ) );
			obj[ei]    = obj[ei].replace( /^s+|s+$/, '' );
		}
		enm.moveNext();
	}
	return obj;
}

This function converts the query string variables collection into a JScript object.

/*    query2obj
 *    transforms Request.QueryString VB collection thing in a JS-friendly object
 *
 */
function query2obj()
{
	var enm    = new Enumerator( Request.QueryString );
	var obj    = new Object();
	var ei, en;
	while( !enm.atEnd() )
	{
		ei    = enm.item();
		if(  Request.QueryString.Item( ei ).Count > 1 )
		{
			obj[ei]    = new Array();
			en    = new Enumerator(Request.QueryString( ei ));
			while( !en.atEnd() )
			{
				obj[ei][ obj[ei].length ]    = en.item();
				en.moveNext();
			}
			en    = null;
		}
		else
		{
			obj[ei]    = new String(Request.QueryString( ei ) );
			obj[ei]    = obj[ei].replace( /^s+|s+$/, '' );
		}
		enm.moveNext();
	}
	return obj;
}
this is what fritz looks like

Email | Resume

This is a post within the site. You can navigate through posts via the links labelled 'next', or click on 'search/sitemap' at the top right handside of the page to navigate more quickly.

RSS

ampersand

Fritz is an Italian chap who's been living and working in London for almost 20 years.

He's currently technical director for an integrated digital agency.

In the past he's been a chef, a musician, and author of comics.

home

Handling dates in Javascript

2006-06-23 15:12 | Tags Javascript / DHTML, HowTo

A few pointers. Comparing Dates If you have two date objects, you need to compare their values returned by their valueOf or ...

Converting a collection to a JScript object in ASP

2006-05-30 13:04 | Tags HowTo, ASP

Enumerators are collections of data that can be iterated through. Although useful, they are

Skewing random numbers towards 0

2006-05-29 11:44 | Tags Coding, HowTo

A quick way to skew random numbers towards 0 is to raise them to some powers - the higher the ...