Groups | Blog | Home
all groups > flash actionscript > september 2007 >

flash actionscript : indexOf associative array?


cayennecode
9/14/2007 11:11:38 PM
var arr:Array = new Array( {food:'Hamburger', soda:'Rootbeer'}, {food:'Pizza',
soda:'7up'} );

trace(arr.indexOf(food:'Hamburger'));

?? I know that doesn't work, but is there syntax to make it work, or does one
have to write their own class to handle this?
cayennecode
9/14/2007 11:15:52 PM
I wonder if performance wise, it'd be better to use a multidimensional array?

var arr1:Array = new Array('Hamburger', 'Pizza');
var arr2:Array = new Array('Rootbeer', '7up');
var arr3:Array = new Array(arr1, arr2);

var index = arr3[0].indexOf('Hamburger');
var soda = arr3[1][index];

lol
kglad
9/14/2007 11:23:44 PM
in your first message arr is an array of objects (that each have two
properties). indexOf will search for array elements that match the parameter
used. in your case, that parameter would be an object, not an object property
and value. you could write another function that returns the object(s) that
match a particular property/value and use that in the indexOf parameter.

your 2nd message contains a different construct for arr3. no longer do you
have objects with a food and soda property. you have an array with foods and
sodas and the foods and sodas have no relationship to each other.
cayennecode
9/14/2007 11:29:07 PM
That's what I understood.

Would your function be a set of loops that finds the passed parameter value?

function(prop):Object
for(var i in AssocArray)
{
for (var j in AssocArray[i])
{
if(AssocArray[i][j]==prop)
{
return AssocArray[i][j];
}
}
}


kglad
9/14/2007 11:37:31 PM
:



function findArrayIndex(prop,value,arr){ // pass prop as a string
for(var i=0;i<arr.length;i++){
if(arr[i][prop]== value){
return i; // returns first matching index
}
cayennecode
9/15/2007 12:10:29 AM
kglad
9/15/2007 12:59:54 AM
gotoAndPlay() and stop() i've been using since flash 4. i started to move
beyond novice in 2002. and i think i've been pretty capable since 2004. and
at this point, i feel confident that if something can be done with
actionscript, i can do it.
cayennecode
9/15/2007 4:57:41 AM
kglad
9/15/2007 5:00:32 AM
cayennecode
9/15/2007 5:13:22 AM
Never been asked to design a 3d bus?? hehe
http://theflashblog.com/

My experience with p3d is that it's very neat what you can do, but it's costly
on the renderer, so you better not have much else going on at the same time...


cayennecode
9/15/2007 5:20:08 AM
It's brutiful !!!!
[code]
var arr:Array = new Array( {name:'myArray', fish:'Beta', cheese:'guda'},
{name:'yourArray', fish:'Trigger', cheese:'Cheddar'} );

function findArrayIndex(prop,value,arr):int
{ // pass prop as a string
for(var i=0;i<arr.length;i++)
{
if(arr[i][prop]== value)
{
return i; // returns first matching index
}
}
return undefined;
}
trace( arr[findArrayIndex('name', 'yourArray', arr)].fish );
[/code]
kglad
9/15/2007 5:29:00 AM
AddThis Social Bookmark Button