PDA

View Full Version : Associative array and indexOf question


blu3
10-14-2009, 08:25 PM
Hi guys,

If i have an associative array like this for example:

var arr:Array = new Array();
arr.push({name:"John", last:"Doe"});
arr.push({name:"Jane", lase:"Doe"});

How can i get array index of elements whose name property is "John" for example without looping through every array element and checking like this:

for(var i:uint=0; i<arr.length; i++)
{
if (arr[i]["name"] == "John")
{
//do smth
}
}

I tried using indexOf which works with normal arrays but i guess not with associative array, i tried the following:

trace(arr.indexOf({name:"John"}));

But that didn't work. Anyone has better idea? Can even indexOf() be used this way with associative arrays?

Thanks in advance,
Best regards

devilmaycry
10-14-2009, 08:58 PM
hello, i dont think that you are using Associative Array (http://www.gskinner.com/blog/archives/2006/07/as3_dictionary.html) here, you need to use the Dictionary class, but also with what you are trying to acheive is not not the case, you may create another class the handles inside the index association...

blu3
10-15-2009, 07:00 PM
Thanks devilmaycry for response, appreciate it!

Best regards