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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function parameter parsing

Status
Not open for further replies.

mickeyg

Technical User
Mar 30, 2001
120
US
I am attempting to write a generic function searcharray.
However when I return from it, my value is undefined.
I am expecting my array length to be 3 but is 9 when I use the alert in my function.
Is anything wrong with my syntax?

Everything works find when I use my hardcoded inline code instead of calling this function.

function searcharray(arrayname, arrayfield, matchvalue, searchfield) {
alert(arrayname.length);
// loop through array to find and return a value
for (var i = 0; i < arrayname.length; i++) {
if (arrayname.arrayfield == matchvalue) {
return arrayname.searchfield;
}
}
}

function dfltdonation() {
<snip>
var mydfltdonation = searcharray(&quot;publarray&quot;, &quot;subscription&quot;, mysub, &quot;dfltdonation&quot;); alert(mydfltdonation);
<snip>
}

Thanks...must be simple I have overlooked :)
Mickey
 
Your 'arrayname' is a string so indeed its length is 9. Remove the quotes in teh call. You want to pass the array not the string 'publarray'.
 
When I call my function I am not finding a match. However, if I just use my commented out loop, I find the correct match.

function dfltreason() {
var mydfltreason = searcharray(publarray,&quot;subscription&quot;,mysub,&quot;dfltreason&quot;);
/****
// loop through publication array to find default reason code
for (var i = 0; i < publarray.length; i++) {
if (publarray.subscription == mysub) {
mydfltreason = publarray.dfltreason;
}
}
****
}

function searcharray(myarrayname,myarrayfield,mymatchvalue,mysearchfield) {
// loop through array to find and return a value
for (var i = 0; i < myarrayname.length; i++) {
if (myarrayname.myarrayfield == mymatchvalue) {
return myarrayname.mysearchfield;
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top