tags:

views:

174

answers:

2

I want to subset an array.I have the following array

public var sellAmount:ArrayCollection=new ArrayCollection([
     {month:"Jan", sell:2000},
     {month:"Feb", sell:5000},
     {month:"Mar", sell:4000},
     {month:"Apr", sell:6000},
     {month:"May", sell:3000},
     {month:"Jun", sell:9000},         
     {month:"Jul", sell:1100},
     {month:"Aug", sell:4000},
     {month:"Sep", sell:1000},
     {month:"Oct", sell:500},
     {month:"Nov", sell:4000},
     {month:"Dec", sell:3000}
     ])

Now based on user requirement I want to select a range of sell data for some month.For example sometime it may be sells data from Apr to Dec,sometime may be Jul-Oct.How can I do that without hampering the original array

A: 

Create a filterFunction for the ArrayCollection which applies the filter as required.

Srirangan
+1  A: 

You can use ArrayCollection.filterFunction, though your use of strings for the month makes it a tad more difficult:

function filterSalesData(startMonth:uint, endMonth:uint) {
    sellAmount.filterFunction = monthRangePredicate;
    sellAmount.refresh();

    function monthRangePredicate(obj : Object) {
        var months : Array = ["Jan", "Feb", "Mar", etc]; // TOOD: promote to class level

        var monthIndex : uint = months.indexOf(obj.month);

        return (monthIndex >= startMonth && monthIndex <= endMonth);
    }
}

filterSalesData(6, 9); // july to october
filterSalesData(3, 11); // april to december
Richard Szalay
Thanks a Lot.But I did it in slightly different way.Instead of using data as ArrayCollection, I used it as array and then I used slice function.