Search code examples
flashactionscript-3

removing items from an array using .splice


I'm trying to remove items from an array but it's not working like its supposed to. Here is my code:

for(var i:uint = 0;i<OrderModel.getInstance().orders.length; i++){
    if (OrderModel.getInstance().orders[i].time == hour){
        OrderModel.getInstance().orders.splice(i, 1);
    }
}

it deletes all the items but 1. I always have one item left which should be deleted but it isn't. Any ideas how this occurs?


Solution

  • Try to use:

    for(var i:int = OrderModel.getInstance().orders.length - 1;i >= 0; i--){
        if (OrderModel.getInstance().orders[i].time == hour){
            OrderModel.getInstance().orders.splice(i, 1);
        }
    }