Flex 循环遍历对象

发布于 2024-08-19 03:53:51 字数 696 浏览 15 评论 0原文

我试图扩展 flex ArrayCollection 以能够搜索包含特定数据的对象并将其返回。

这是我的函数:

public function getItemContaining(value: String): Object {                      
          //Loop through the collection         
          for each(var i: Object in this) {                             
            //Loop through fields                               
            for(var j: String in i) {                   
                //If field value is equal to input value
                if(i[j] == value) {
                    return i;

                }
            }
        }
    //If not found
    return null;
    }

问题是 j 始终为空,因此第二个循环永远不会工作。所以我阅读了柔性循环描述,实际上它应该工作得很好。可能是什么问题?

Im trying to extend the flex ArrayCollection to be able to search for an object containing specific data and give it back.

Here is my function:

public function getItemContaining(value: String): Object {                      
          //Loop through the collection         
          for each(var i: Object in this) {                             
            //Loop through fields                               
            for(var j: String in i) {                   
                //If field value is equal to input value
                if(i[j] == value) {
                    return i;

                }
            }
        }
    //If not found
    return null;
    }

Problem is j is always null so the second loop never works. So I read flex loop descriptions and actually it should work just fine. What can possibly be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永言不败 2024-08-26 03:53:51

尝试这样:

for (var name:String in myObject){
  trace(name + ":" + myObject[name];
}

好吧,这实际上与您所做的相同。错误必须在这一行:

for each(var i: Object in this) {

尝试使用此:

for each(var i: Object in this.source) {

Try it like this:

for (var name:String in myObject){
  trace(name + ":" + myObject[name];
}

Okay that was actually the same you were doing. The error must be in this line:

for each(var i: Object in this) {

Try using this:

for each(var i: Object in this.source) {
我很坚强 2024-08-26 03:53:51

我的第一直觉是查看数据类型。您正在设置一个声明 j:String 的循环,症状是 j 始终为 null。这对我来说表明 Flex 无法将 i 的元素解释为字符串。如果 Flex 仅将 i 的元素识别为对象(因为所有字符串都是对象,而对象是最小公分母),则它会为 j:String 返回 null。

尝试一下你的内循环:

for(var j: Object in i) {                   
    //If field value is equal to input value
    if(i[j] is String && (i[j] as String) == value) {
        return i;
    }
}

My first instinct would be to have a look at data type. You're setting up a loop declaring j:String and the symptom is that j is always null. This suggests to me that Flex is failing to interpret the elements of i as strings. If Flex only recognizes the elements of i as Objects (because all Strings are Objects, and Objects are the lowest common denominator), it would return null for j:String.

Try this for your inner loop:

for(var j: Object in i) {                   
    //If field value is equal to input value
    if(i[j] is String && (i[j] as String) == value) {
        return i;
    }
}
栀子花开つ 2024-08-26 03:53:51

如果您使用 ArrayCollection 作为数据源,则应该考虑使用 IViewCursor 接口。您可以提供自定义比较函数,或提供顶部比较的字段。该界面在 adobe/livedocs 中有详细的示例记录,

var _cursor:IViewCursor;
var _idSortField:SortField;
var _idSort:Sort = new Sort();
_idSortField = new SortField();
_idSortField.compareFunction = this.myCompareFunction; 
_idSort.fields = [_idSortField];
myArrayCollection.sort = _idSort;
myArrayCollection.refresh();
_cursor = myArrayCollection.createCursor();
if (_cursor.findAny(search))
   return _cursor;

如果您要搜索特定属性中的值,那么它会更容易。以下是有关此内容的 adobe livedocs 的链接话题

if you are using ArrayCollection as your datasource, you should look at using the IViewCursor interface. You can supply a custom compare function, or supply the fields top compare to. This interface is well documented with examples in adobe/livedocs

var _cursor:IViewCursor;
var _idSortField:SortField;
var _idSort:Sort = new Sort();
_idSortField = new SortField();
_idSortField.compareFunction = this.myCompareFunction; 
_idSort.fields = [_idSortField];
myArrayCollection.sort = _idSort;
myArrayCollection.refresh();
_cursor = myArrayCollection.createCursor();
if (_cursor.findAny(search))
   return _cursor;

if you are search for a value in a specific property, then its even easier. Here's the link to adobe livedocs on this topic

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文