msg1 {
fjacquier@babel.fr[+],
posted: 04.06.01 11p•-,
top [^]
}
could you find something similar for multiple names took separately ?
msg2 {
paulw[+],
posted: 04.06.01 11p•-,
top [^]
}
How about extending this so you can choose any property to search on, not just .name
<code>
// a find feature for an object array
// search by any property
Array.prototype.findByValue = function(prop,value) {
for(var i = 0;i<this.length;i++) {
if(this[i][prop] == value) {
return this[i];
}
}
}
// you might use it like this:
myArray = [{name:"bob",town:"London"},{name:"hans",town:"munich"},{name:"manuel",town:"barcelona"}];
foo = myArray.findByValue("town","barcelona");
trace(foo.town + "\n" + foo.name);
</code>
msg3 {
plasticbrain[+],
posted: 07.09.03 12a•-,
top [^]
}
You can significantly (about 10% faster) speed up this function by using a while loop instead:
///////////
Array.prototype.findByValue = function(prop, value) {
var i = this.length;
while (i--) {
if (this[i][prop] == value) {
return this[i];
}
}
};
///////////
I'm disappointed with the speed of accessing object arrays in Flash. Acessing property lists in Director is much faster. Is there a good, fast way of implementing a property list class in Flash?
msg4 {
senocular[+],
posted: 07.09.03 3a•-,
top [^]
}
you have while loops, for loops and for..in loops.
In terms of speed, while and for loops are the same. The difference between them would be your handling of variables and conditions. testing for a single character variable like i is less time than comparing someObject.someProperty like anArray.length. Subtraction is also commonly faster than addition and pre-increment is commonly faster than post-increment. So value - (-value) is faster than value + value and --value is faster than value++. Of course, then again, in terms of speed you would rather use v instead of value since Flash parses variable names so the smaller the name the less time it takes to evaluate. With this, dot syntax is faster than associative array referencing i.e. myObject.property is faster than myObject["property"].
for..in loops are generally faster than while and for loops because they dont involve an incremental value - no i++ or whathaveyou for each iteration of the loop. They just throw all the properties of that object on a stack (in an order based on creation - most recently created first) then just pull from that stack. This, however, can be slower than for and while for very large arrays. Also, if you're dealing with arrays, you are also grabbing all other non-numeric properties of that array and not just those values within the range of 0-array.length-1. So if you assigned some properties like .name or .type to an array object, those values would be thrown in to the iteration of a for..in loop while a for or while loop based on an array's length would not. Of course if you're not accessing an array and rather an object object, then a for..in is about your only option unless you've previously listed the properties you want to enumerate through beforehand.