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

problem of getIndex function number 2

Status
Not open for further replies.

duponty

Instructor
Oct 25, 2000
101
CA
Thanks first to everybody who help me for my get Index function, It was working except for on thing:

when you create an array like:

myArray = new Array()
myArray["first"]= image1.gif
myArray["second"]=image2.gif
myArray["third"]= image3.gif

I cannot use your functions to get the index with the reference "first". Actually, you can but it seams the the index of "first", "second" and "third" are all three of value 0. Why is that?

thanks again
Yannick
 
I think lucid, or iza mentioned somrthing about this, you will need to prototype the Array object if you want it to handle both. So do you have a function fro retreiving the index by matching the values?
Normally you want to match a value in the array, and return the index right, like above you would try to find the index of "image1.gif"?

So is the point here that you want to be able to reference by string index, but still be able to have integer index available too? The stuff below just makes a small change to the way elements are added to the array, but in return enables you to ask for the index of an element by either value or string index.

Array.prototype.addItem = addItem;
// sample array - showing how to add elements
var people = new Array();
people.addItem('bangers',"Ben");
people.addItem('gas',"Gus");
people.addItem('tony',"Tim");

// This function packages up the string index and the value into a vector, which
// is then stored in the Array. Another function will look at both of these and
// return the corresponding index, if one matches.
function addItem(stringIndex,DATA){
var length = this.length;
var entryVector = new Array();
entryVector[0] = stringIndex;
entryVector[1] = DATA;
this[(length)] = entryVector;
}


function getIndex(member,array){
var index = -1 ;
if(array[0][0]){
for(var j=0;j<array.length;j++){
for(var k=0;k<2;k++){
// matches either string index or value.
if(array[j][k].indexOf(member.toLowerCase())!= -1
||
array[j][k].indexOf(member) !=-1)
index=j;
}
}

}
//ordinary array.
else{
for(var j=0;j<array.length;j++){
if(array[j] == member){
index = j;
break;
}
}
}
return index;
}


This does not enable you to access elements by string index, though, that requires an aditional function, which I have not written yet - but you can get them by integer index, so here people[0] --> people[0][1].
b2 - benbiddington@surf4nix.com
 
With a small modification, and one more method added to the Array object, we can now obtain elements by both string/integer index, and still have the functionality we found before:

Array.prototype.addItem = addItem;
Array.prototype.getItem = getItem;

function addItem(stringIndex,DATA){
var length = this.length;
var entryVector = new Array();
entryVector[0] = stringIndex;
entryVector[1] = DATA;
this[(length)] = entryVector;
// add string indexability
this[stringIndex] = entryVector;

}

function getItem(index){
var value;
check = this[index];
if(check != null)value = this[index][1];
else value = 'bad index';
return value;
}


Ofcourse this just takes away the need to get elements by using array[string/int][1]. By prototyping, we have made these methods applicable only to arrays, which may or may not be handy in some situations,I have not fully thought it thru. ;-)

syntax: ...onClick=&quot;alert(people.getItem('gas'))&quot; ...

returns - Gus

...onClick=&quot;alert(people.getItem(1))&quot; ...

also returns Gus



b2 - benbiddington@surf4nix.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top