|
#1
|
|||
|
|||
|
An array / index issue
Hi,
Does someone know an array method (or something else) that returns the index of an element of this array. For instance : array[0]="joe"; array[1]="nono"; array[2]="dudul"; array[3]="titi"; If I ask to this "method" the index of the element "joe", I expect it to return 0. If I ask to this "method" the index of the element "dudul", I expect it to return 2. It sounds easy... did not find the method through the actionscript dictionnary though... Thanks for your help |
|
#2
|
|||
|
|||
|
try something like this
Actionscript:
function getindex (name){
for (i=0;i<array.length;++i){
if (name==array[i]){
nameindex=i;
}
}
getindex("dudul"); |
|
#3
|
||||
|
||||
|
Heres an alternative:
Actionscript:
// every array you create will now have this function
Array.prototype.getIndex = function(searchWord) {
var indexArray = new Array();
for (var counter in this) {
if (this[counter] == searchWord) {
indexArray.push(counter);
}
}
return indexArray;
};
//you must define your array as new Array() for it to work
myArray = new Array();
myArray[0] = "joe";
myArray[1] = "nono";
myArray[2] = "dudul";
myArray[3] = "titi";
myArray[4] = "dudul";
//the following lines are alternate methods of defining the array that also work
//myArray = new Array("joe","nono","dudul","titi","dudul");
//myArray = ["joe","nono","dudul","titi","dudul"];
//will give you the an array with the index of every value that is "dudul"
myResults = myArray.getIndex("dudul");
trace (myResults);This can be extended to give all array indices that contain the searchWord... Actionscript:
Array.prototype.getIndex = function(searchWord) {
var indexArray = new Array();
for (var counter in this) {
if (this[counter].toString().indexOf(searchWord)!= -1) {
indexArray.push(counter);
}
}
return indexArray;
};
myArray = new Array();
myArray[0] = "joe";
myArray[1] = "nono";
myArray[2] = "dudul is nice";
myArray[3] = "titi";
myArray[4] = "dudul is crazy";
myResults = myArray.getIndex("dudul");
trace (myResults);The for (var watever in myObject) code is nice because its simpler than the alternative for(var i = 0; i<myArray.length;i++). It is also very nice in cases where you define the values of your array without using simple numbers (e.g. myArray["bob"]) because these values are hard to access through the conventional for loop. Using a for(..in..) loop, however, it access EVERY value in the array. Unfortunately, this can also be a negative. In the case of my code, myArray.getIndex is found also, even though it is not really part of the content of my array(which sux). that is why I had to add the .toString part to the second code example I gave... |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Play Coordinated objects in Array | heimer22 | Flash MX |
2 | 07-07-2004 10:55 PM |
| Resolved: display contents of an array | Lelboy | Flash MX |
2 | 05-06-2004 03:46 PM |
| trouble sorting dynamic Array | RacerX | Flash MX ActionScript |
2 | 12-05-2003 09:24 PM |
| Problems reading from an Array | dogzi | Flash 5 Actionscript |
4 | 07-09-2001 03:59 PM |
All times are GMT. The time now is 09:59 PM.



TODAY'S POSTS
Flash Coding



