0

나는이 내가 동적 같은 맨 아래에 만든 arrayCollection : ID로 그룹 데이터에어떻게 그룹화 SQL과 같은 AS3에있는 ArrayCollection을 생성 할 수 있습니다

arrCol = ({"ID":ids[i][0], "Price":ids[i][1], "OtherInfo":ids[i][2]}); 

내가 원하는 요약 Price을.

ArrayCollection는 SQL 테이블 인 경우, 나는이 같은 쿼리 사용할 수 있습니다 : 그래서

SELECT ID, SUM(Price), OtherInfo 
FROM TableA 
GROUP BY ID 

을 어떻게 SQL에서 쿼리의 예와 같은 AS3 기능을 설정하거나이이에 대한 기본 ArrayCollection 클래스 수 ?

+0

AarrayCollection 클래스는 그러한 기능이 없다. 배열을 반복하고 합계를 변수에 저장해야합니다. – hidarikani

답변

0

이 기능을 사용해보십시오. 필요에 따라 사용할 수있는 기능이 없습니다 (sum, groupby). 아래 코드를 수동으로 수행해야 할 필요가 있습니다.

var arrCol:ArrayCollection = new ArrayCollection(); 

arrCol.addItem({"ID":1, "Price":100, "OtherInfo":"info"}); 
arrCol.addItem({"ID":1, "Price":700, "OtherInfo":"info"}); 
arrCol.addItem({"ID":2, "Price":100, "OtherInfo":"info"}); 
arrCol.addItem({"ID":2, "Price":200, "OtherInfo":"info"}); 
arrCol.addItem({"ID":3, "Price":100, "OtherInfo":"info"}); 
arrCol.addItem({"ID":3, "Price":400, "OtherInfo":"info"}); 
arrCol.addItem({"ID":3, "Price":100, "OtherInfo":"info"}); 

var dic:Dictionary = new Dictionary(); 

for each (var item:Object in arrCol) 
{ 
    if(!dic[item.ID]){ 
     dic[item.ID] = item; 
    } 
    else{       
     var oldSumObj:Object = dic[item.ID]; 
     oldSumObj.Price +=item.Price; 
     dic[item.ID] = oldSumObj; 
    } 
} 

var groupedList:ArrayCollection = new ArrayCollection(); 

for each (var itemObj:Object in dic) 
{ 
    groupedList.addItem(itemObj); 
} 

출력 될 것이다

"groupedList" mx.collections.ArrayCollection (@27af939) 
    [0] Object (@8836569) 
     ID 1 
     OtherInfo "info" 
     Price 800 [0x320] 
    [1] Object (@87a7c71) 
     ID 2 
     OtherInfo "info" 
     Price 300 [0x12c] 
    [2] Object (@87a7bc9) 
     ID 3 
     OtherInfo "info" 
     Price 600 [0x258] 
0

AS3에서 SQL 형식 쿼리를 만들 수는 없지만 동일한 형식의 메서드를 사용하여 동일한 결과를 얻을 수 있습니다.

// Work with an array. Child elements must be an associative array (structure/object/hash-table) 
var ac:Array = [ 
    {"name":"apple", "price":100, "color":"red"}, 
    {"name":"banana", "price":50, "color":"yellow"}, 
    {"name":"pear", "price":250, "color":"green"}, 
] 

// Apply the order your want based on the property you're concerned with. 
ac.sortOn("price", Array.ASCENDING) 

// If necessary, create a subset of that array with the "select"-ed columns. 
var output:Array = []; 
for each (var entry:Object in ac) { 
    output.push({entry.name, entry.color}); 
} 

// Printing output will result in 
// 0:{"name":"banana", "color":"yellow"}, 
// 1:{"name":"apple", "color":"red"}, 
// 2:{"name":"pear", "color":"green"} 
관련 문제