检查对象并返回是不是找到该元素

Posted

技术标签:

【中文标题】检查对象并返回是不是找到该元素【英文标题】:Checking through an Object and returning if the element is found检查对象并返回是否找到该元素 【发布时间】:2016-01-22 01:21:25 【问题描述】:

尽管对象中的某些数组在其中包含多个元素,但我试图为在对象中找到的项目返回 true。例如 selectedEventTypes[9] 有一个 [cancelled,cancelled2,cancelled3] 数组,尽管当 item.eventType 被 cancelled2 时,它永远不会意识到它在 Object 中并且总是为此返回 false。

private function eventFilterFucntion(item:Object):Boolean
   
    if(selectedEventTypes.indexOf(item.eventType)>-1)
    
        return true;
    
    return false;

【问题讨论】:

您的代码看起来不错。您能否展示一下您是如何创建selectedEventTypes 数组和item 对象的? 【参考方案1】:

您似乎已将 selectedEventTypes 定义为 ArrayArray,例如:

private var selectedEventTypes:Array = [["A1", "A2"],
        ["B1","B2"],
        ["C1","C2"],
        ["D1","D2"],
        ["E1","E2"],
        ["F1","F2"],
        ["G1","G2"],
        ["H1","H2"],
        ["I1","I2"],
        ["Cancelled", "Cancelled2", "Cancelled3"],
        ["Received","Last"]];

在哪里

selectedEventTypes[9] = ["Cancelled", "Cancelled2", "Cancelled3"]

在这种情况下,您可以如下更改您的 eventFilterFunction

private function eventFilterFucntion(item:Object):Boolean
    
        for each(var eachArray:Array in selectedEventTypes)
        
            if(eachArray.indexOf(item.eventType) > -1)
            
                return true;
            
        
        return false;
    

试试这个示例应用程序:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script><![CDATA[
    import mx.collections.ArrayCollection;
    private var selectedEventTypes:Array = [["A1", "A2"],
        ["B1","B2"],
        ["C1","C2"],
        ["D1","D2"],
        ["E1","E2"],
        ["F1","F2"],
        ["G1","G2"],
        ["H1","H2"],
        ["I1","I2"],
        ["Cancelled", "Cancelled2", "Cancelled3"],
        ["Received","Last"]];
    [Bindable]
    private var myArrayCollectionOriginal:ArrayCollection = new ArrayCollection([
        name:"State 1",eventType:"Submitted",
        name:"State 2",eventType:"Cancelled2",
        name:"State 3",eventType:"Pending",
        name:"State 4",eventType:"Cancelled3",
        name:"State 5",eventType:"Failed",
        name:"State 6",eventType:"Received"
        ]);
    [Bindable]
    private var otherArrayCollection:ArrayCollection = new ArrayCollection();
    private function applyFilterButtonClicked():void
    
        otherArrayCollection = new ArrayCollection(myArrayCollectionOriginal.source)
        otherArrayCollection.filterFunction = eventFilterFucntion;
        otherArrayCollection.refresh();
    

    private function eventFilterFucntion(item:Object):Boolean
    
        for each(var eachArray:Array in selectedEventTypes)
        
            if(eachArray.indexOf(item.eventType) > -1)
            
                return true;
            
        
        return false;
    
    ]]></fx:Script>
<s:HGroup verticalCenter="0" horizontalCenter="0">
    <s:Panel title="Original collection">
        <s:DataGrid dataProvider="myArrayCollectionOriginal"/>
    </s:Panel>
    <s:Button click="applyFilterButtonClicked()" label="Apply Filter" />
    <s:Panel title="Filtered Output">
        <s:DataGrid dataProvider="otherArrayCollection"/>
    </s:Panel>
</s:HGroup>

</s:Application>

【讨论】:

以上是关于检查对象并返回是不是找到该元素的主要内容,如果未能解决你的问题,请参考以下文章

Angular 7检查JSON解析是不是返回特定对象的正确方法[关闭]

查找是不是在列表中找到具有特定属性值的元素

如何返回从检查数组中匹配的对象元素的索引

javascript 找到应用于特定元素的所有CSS规则;并检查样式表中是否定义了特定元素的CSS属性 - 而不是

如何检查字符串是不是包含列表的任何元素并获取元素的值?

从一个表中选择所有元素,并检查它们是不是与另一个表匹配