0

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

4
  • 1
    Show the array structure. Commented Jun 9, 2017 at 9:29
  • can you post the sample array? not the image. Commented Jun 9, 2017 at 9:29
  • My array structure is like this internally:Array[0]24: Array[1]0: Object CommercialReference: "P7M18012" Description: " User manual: Maintainance guide" ListPrice: 156.37 PurchaseQty: 1 Quantity: 1 Supplier: " AMT " TotalCost: 156.37 Commented Jun 9, 2017 at 9:36
  • So if this quantity element is zero , I want this complete array to be removed from the list. Commented Jun 9, 2017 at 9:36

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot...it worked. I am getting the expected results.
I'm glad that I could help:)
0

Use this it maybe it will help you.

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

Comments

0

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!

2 Comments

Do you mean a.push means indexex.push(index)? and for (i in index) { $scope.splice(i, 1); this is where I need to give $scope.firstorder.splice? }
I mean just store them and afterwards use the indexes to delete the arrays that you want to delete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.