仅具有属性值的AS3对象列表搜索

时间:2010-10-16 18:09:57

标签: flex flash arrays actionscript-3 object

假设我有一个特定自定义类型的对象列表,我需要找到一个只给出其某个属性值的对象,我该怎么办?

EG。

  // 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 个答案:

答案 0 :(得分: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()方法有很多选项;一定要阅读文档。