Reputation: 9
I have a group of arrays in $scope.firstorder
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;
}
}
Upvotes: 0
Views: 343
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
Reputation: 502
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
Reputation: 1192
Use this it maybe it will help you.
$scope.block = $scope.block.filter(function (arr) {
return arr.length;
});
Upvotes: 0