Click here to Skip to main content
Licence 
First Posted 31 Mar 2002
Views 38,998
Bookmarked 9 times

Working with lists

By | 31 Mar 2002 | Article
A couple of functions to wrap the process of working with lists.

Introduction

Recently, I have been doing a great deal of web development. Because of which, I decided to create an API set for the more commonly used functions I routinely needed to implement in JavaScript. This article contains two functions from my API set that may be of interest to web developers at large.

These functions wrap the process of adding, removing, and replacing items in a list -- a string with delimited indexes using a special character. Here are just a couple of instances where you might want to use these functions... If you must, for some reason, use JavaScript 1.0 this can be a lifesaver because arrays didn't exist in that version. If you wish to work with multiple values in a single cookie, this will also prove to be severely useful (at least it does for me).

The two functions involved here are SetInList() and GetInList(). As you might imagine, SetInList() will either insert or replace an index in a list and GetInList() retrieve an index from a list.

SetInList() takes five parameters:

list = string containing the actual list
item = the value to place in the list
index = the index to be set
delim = the delimiter used to create the list
insert = whether or not to insert or replace the item (boolean)

GetInList() takes three parameters:

list = string containing the actual list
index = the index to be set
delim = the delimiter used to create the list

NOTE: The indexes are intended to be zero-based, and if the index cannot not be found in the list both functions will return false.

Example...

<script language="JavaScript" src="list.js"></script>
<script language="JavaScript"><!--
	var list = "apples,oranges,pineapples,bannanas,peaches";
	document.writeln("1 = \"" + list + "\"<br>");

	list = SetInList(list, "grapes", 2, ",", true);
	document.writeln("2 = \"" + list + "\"<br>");

	list = SetInList(list, "tangerines", 4, ",", false);
	document.writeln("3 = \"" + list + "\"<br>");

	document.writeln("4 = \"" + GetInList(list, 3, ",") + "\"");
//--></script>

Will produce the output...

1 = "apples,oranges,pineapples,bannanas,peaches"
2 = "apples,oranges,grapes,pineapples,bannanas,peaches"
3 = "apples,oranges,grapes,pineapples,tangerines,peaches"
4 = "pineapples"

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jeremy Falcon

Software Developer (Senior)
Enta USA, LLC
United States United States

Member

Jeremy finds it odd to mention himself in the third person, but alas, since nobody else can relish in the delight of Jeremy's marvelous accolades on this page, he must. And since embellishment is the way of self-centric, third-person style of writing in any good piece, it will be accepted as Jeremy's duty to master the sublime art of conceit.
 
Jeremy has conquered cancer just by writing five lines of code. Unfortunately, the government disallows that code to be revealed to the populous. Jeremy also commands time and space with his facial hair. Jeremy thrice resurrected the dead by using undocumented APIs. Jeremy can read your mind too, simply because that's just how über Jeremy is.
 
What, you don't believe that? Ok, take two...
 
Jeremy is someone who understands the importance of doing his part in giving back to the online community at large. He has been tinkering with electronics and computers in general since circa 1986, but he has studied development since 1994. Over the years he has acquired somewhat of a diverse skill set - which ranges from graphics, to technician, to programmer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 1 Apr 2002
Article Copyright 2002 by Jeremy Falcon
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid