我想在flex中基于字符串分离arraycollection。
我的数组收集项看起来像{[(name:aa),(name:bb),(name:ae),(name:cc),(name:bd)}]
,
现在我想基于'a'和所有'a'项来分离这个arraycollection,以便在单独的arraycollection中添加。
像
一样的Arraycollection声明[Bindable]
public var arr:ArrayCollection=new ArrayCollection([{name:"cards/a.png"},{name:"cards/b.png"},{name:"cards/ac.png"},{name:"af.png"},{name:"ad.png"},{name:"cards/bb.png"}]);
答案 0 :(得分:0)
您的arraycollection结构应为new ArrayCollection([{name:"aa"},{name:"bb"}]);
“(”它应该是“{”。
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.utils.ObjectUtil;
public var wordCollection:ArrayCollection = new ArrayCollection([{name:"aa"}, {name:"bb"},
{name:"ae"}, {name:"cc"}, {name:"bd"}]);
public var charACollection:ArrayCollection = new ArrayCollection();
public var charBCollection:ArrayCollection = new ArrayCollection();
public var charCCollection:ArrayCollection = new ArrayCollection();
private function createCompleteHandler():void
{
wordCollection.filterFunction = splitByChars;
wordCollection.refresh();
trace(ObjectUtil.toString(charACollection));
trace(ObjectUtil.toString(charBCollection));
trace(ObjectUtil.toString(charCCollection));
}
private function splitByChars(item:Object):Boolean
{
var char:String = item.name.toString().charAt(0);
if (char.toLowerCase() == "a")
{
charACollection.addItem(item);
}
else if (char.toLowerCase() == "b")
{
charBCollection.addItem(item);
}
else if (char.toLowerCase() == "c")
{
charCCollection.addItem(item);
}
return true;
}
]]>
</fx:Script>
</s:Application>