AS3 Object List Search with Attribute Value Only
Suppose I have a list of objects of a certain custom type and I need to find an object given only the value of one of its attributes, how do I do it?
Eg.
// Where user1 ... usern are objects of some class User
users = [ user1, user2, user3 ... usern ]
// How do I find out the o开发者_开发百科bjects that have the "foo" attribute set to "bar"
You can use the filter()
method of the array:
var fooBarUsers:* = users.filter(function (user:User) {
return user.foo == "bar";
});
To sort based on an attribute, use the sortOn
method:
fooBarUsers.sortOn("foo"); // Sorts in-place by default
There are lots of options for the sortOn()
method; be sure to read the documentation.
精彩评论