2010-10-16 72 views
0

假设我有一个特定自定义类型的对象列表,并且我只需要找到一个只给出其某个属性值的对象,那么我该怎么做?仅具有属性值的AS3对象列表搜索

例如,

// Where user1 ... usern are objects of some class User 
    users = [ user1, user2, user3 ... usern ] 

    // How do I find out the objects that have the "foo" attribute set to "bar" 

回答

1

您可以使用数组的filter() method

var fooBarUsers:* = users.filter(function (user:User) { 
    return user.foo == "bar"; 
}); 

排序依据的属性,使用sortOn method

fooBarUsers.sortOn("foo");   // Sorts in-place by default 

有很多的选择为sortOn()方法;一定要阅读文档。

+0

谢谢。如果我需要根据属性进行排序,那么我该怎么做? – 2010-10-16 19:05:21

+0

@Yasmin:查看我编辑的答案 – Cameron 2010-10-16 19:20:49

+0

明白了。谢谢=) – 2010-10-16 22:39:09

相关问题