开发者

AS3 Object List Search with Attribute Value Only

开发者 https://www.devze.com 2023-01-20 15:06 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消