Splitting Arrays

A

Andi B

If I have an array set up like so to contain a small code, and the name of
the person to whom the code relates, the values split by a comma:

DMCName[0]="1SC,Andrea Pidgeon"

and I want to be able to return the name when someone enters the code into a
text box, is there a way to split the array and only return the name? -
Bearing in mind that there will be more than one code, and other details
will be included besides the name of the person, and that such a method of
splitting would have to be able to scan through all (roughly 40 entries) in
the array and compare the code to the users entry...
 
G

Grant Wagner

Andi said:
If I have an array set up like so to contain a small code, and the name of
the person to whom the code relates, the values split by a comma:

DMCName[0]="1SC,Andrea Pidgeon"

and I want to be able to return the name when someone enters the code into a
text box, is there a way to split the array and only return the name? -
Bearing in mind that there will be more than one code, and other details
will be included besides the name of the person, and that such a method of
splitting would have to be able to scan through all (roughly 40 entries) in
the array and compare the code to the users entry...

The split() method returns an array:

var splitStuff = DMCName[0].split(/,/);
var userInput = "whatever";

for (var i = 0; i < splitStuff.length; i++) {
if (splitStuff == userInput) {
// you found a match
break;
}
}

If you know the name is always the second thing in the string, you could also do
something like:

if (DMCName[0].split(/,/)[1] == userInput) {
// found a match
}

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top