Shweta Singh
Shweta Singh

Reputation: 9

How to delete array from a group of array?

I have a group of arrays in $scope.firstorder

Image of array group.

Based on some condition e.g. array contains an element Quantity. If Quantity is zero I need to remove this array from the list of arrays.

How can I do that?

Here is my code:

for (index in $scope.firstorder)
{
var quantity = $scope.firstorder[index][0].Quantity;
if (quantity == 0)
{
    //Remove the array from $scope.firstOrder;
}

}

Image of group of arrays

Upvotes: 0

Views: 343

Answers (3)

Naruto
Naruto

Reputation: 708

You can use filter combined with Array functions.

$scope.firstorder = $scope.firstorder.filter(outerList=>{ 
      return outerList.filter(innerList=>{ 
          return innerList.Quantity === 0;
        }).length === 0; 
    });

If you want to modify existing array, you can "splice" after finding the index of the arrays which should be deleted.

Thanks.

Upvotes: 1

I think that modifying structures on the time when you iterate them is a bad practice (even though possible).

I would save all the indexes affected and remove them later on (maybe not so effective, but secure).

var indexex = [];
for (index in $scope.firstorder) {
    var quantity = $scope.firstorder[index][0].Quantity;
    if (quantity == 0) {
       a.push(index);
    }
}
for (i in index) {
    $scope.splice(i, 1);
}

(I am not sure the code is perfect, I coded it directly here, check it with an IDE, but I think the idea is there)

Hope it helps!

Upvotes: 0

Narendra Solanki
Narendra Solanki

Reputation: 1192

Use this it maybe it will help you.

$scope.block = $scope.block.filter(function (arr) {
    return arr.length;
});

Upvotes: 0

Related Questions