Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array elements: how to split?

Status
Not open for further replies.

aprilius

Programmer
Apr 23, 2002
7
0
0
US
i am just not as up on javascript as i should be, so i am begging someone for help.

i have a parent window with a form on it. in order to keep this form on one page, i have put buttons next to each text field on the parent form that launch a pop-up window. that pop-up window queries a database and builds a form from the resultset (in coldfusion).

after selects one or more checkboxes, the form gets submitted back to itself. i change the coldfusion form values (comes as a string) into a javascript variable.

the string comes in the following format:

allen:Allen|Debbie, baker:Baker|April, ellis:Ellis|Joan

the word before the colon is the nt username, the next word is the last name, and the third word (after the pipe character) is the first name. each set pertaining to one user is delimited by a comma. i do not know how many "sets" of data i am going to get, because users could check any number of boxes.

i did a split function on the comma on the incoming string, and stuffed the results into an array.

now i need to somehow separate the nt user name (part before the colon) from the last name and first name, and stuff the list of nt user names into its own array, and the last name/first name into another. i want to return the nt user names to a text box in the parent window, and the last name/first name to another text box in the parent window. that's the reason i need to split it out somehow. argh.

how can i do this?

this is all of the javascript i have written so far. i can get the results back to the parent form, just not the way i want them. (the variable in the pound signs is how you refer to a coldfusion variable. don't let it throw you):

Code:
<script language = &quot;javascript&quot;>
var names = '#form.chkAssignees#';
var names_array = names.split(&quot;,&quot;);
  for (var loop=0; loop < names_array.length; loop++)
	{
    	alert(names_array[(loop]);  //this is debug code
	}

opener.document.frmAddTask.txtTaskAssignees.value = names_array;
self.close();
</script>

gratefully yours,
april
 
you can split once again the names. let's see if I understood this :

var ntUserNames = new Array();
var firstNames = new Array();
var lastNames = new Array();

var namesArray = &quot;allen:Allen|Debbie, baker:Baker|April, ellis:Ellis|Joan&quot;.split(',');

for (var i = 0; i < namesArray.length; i++)
{
var ntFnLn = namesArray.split('|')

var ntAndFn = ntFnLn[0].split(':')
var ln = ntFnLn[1]
var nt = ntAndFn[0]
var fn = ntAndFn[1]

ntUserNames.push(nt)
firstNames.push(fn)
lastNames.push(ln)
}


for (var i = 0; i < ntUserNames.length; i++)
{
// change this to do whatever you want with it.
alert(ntUserNames + &quot; &quot; + firstNames + &quot; &quot; + lastNames)
}


Hope this helps. I love these questions! :) Gary Haran
 
Without using objects:

var userID=new Array(), lfname=new Array();

for (li=0;li<names_array.length;li++)
{
var oneuser=names_array[li];
userID[li]=oneuser.substr(0, oneuser.indexOf(':'))
lfname[li]=oneuser.substr(oneuser.indexOf(':') + 1))
}

 
I figured out where the dots came from, using
Code:
[li]
as my index counter.

var userID=new Array(), lfname=new Array();

for (loop=0;loop<names_array.length;loop++)
{
var oneuser=names_array[loop];
userID[loop]=oneuser.substr(0, oneuser.indexOf(':'))
lfname[loop]=oneuser.substr(oneuser.indexOf(':') + 1))
}

 
This is using an object, and sorting out ID, first, and last names separately:

function User(user)
{
this.userID = user.substr(0,user.indexOf(':'));
this.last = user.substring(user.indexOf(':') + 1, user.indexOf('|')-1);
this.first = user.substr(user.indexOf('|')+1);
return this;
}

var user=new Array();

for (loop=0;loop<names_array.length;loop++)
{
var user[loop]=new User(names_array[loop]);
}



 
many thanks to all! now i can move on with my life! i sure appreciate the help.

april
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top