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

flash actionscript : Search Array with multiple values


sbryner
9/27/2006 4:06:48 PM
Hello,

I've been helped with the prototype below by blemmo (thanks).

It allows me to search my array using one property == a value.

Now, I'd like to use it to:

if ((prop1 == val1) &&(prop2 == val2) && (prop3 == val3) {
// search array for all values in array with the values of val1, val2 and val3
};

Also, a way to search if one of the prop and val's are blank, ie...

((prop1 == val1) &&(prop2 == val2) && (prop3 == val3)
where prop2 and val2 are "undefined"

so the search will only search for the prop and val that are not "undefined"

make since? Don't know where to start.
help and thanks in advance.



Array.prototype.id = function(prop, val) {
var res:Array = new Array();
var i = this.length;
while (i--) {
if (this[i][prop] == val) {
res.push(this[i]);
} else {

}
}
return (res);
};
kglad
9/29/2006 3:57:15 AM
try the following which returns the index of the element whose properties match
your values:





Array.prototype.id = function(prop1, val1, prop2, val2, prop3, val3) {
var res:Array = new Array();
var i = this.length;
while (i--) {
if (this[i][prop1] == val1 && this[i][prop2] == val2 && this[i][prop3] ==
val3) {
res.push(i);
} else {
}
}
return (res);
};
sbryner
10/4/2006 3:28:56 PM
thanks Kglad but it's not working for me. I thought to do that and tried it
already but it seems to not pull up any info from my array and place them into
the respective dynamic text boxes. I just places, "undefined" in all. though
when I use the original code it works fine. I'm not sure why. I believe I've
done everything correct. I'll try to keep hammering it out, believing I was on
the correct coding path.

thanks again,

sky
kglad
10/4/2006 8:53:15 PM
sbryner
10/5/2006 10:18:23 PM
it is an array like this.. but with more properties... also, is it better to
use XML to load large arrays or is it ok to use actionscript in the file.

which is faster to load? say if I had an array of 150 elements?




myArray = [{myname:"bob", county:"skagit", city:"Sedro Woolley", id:1,
pic:"1.jpg",pic1:"2.jpg",pic2:"3.jpg",pic3:"4.jpg"},
{myname:"bob", county:"skagit", city:"Sedro Woolley", id:1,
pic:"1.jpg",pic1:"2.jpg",pic2:"3.jpg",pic3:"4.jpg"
kglad
10/6/2006 2:50:39 AM
then everything should work. are you using strings for the parameters passed to id()???



AddThis Social Bookmark Button