0

I have issues removing items from an array based on matched value. I have an array of object like below:

$scope.listOfItems = [
  {productID : 1, weight: 3.5, price: 5},
  {productID : 2, weight: 4.5, price: 8},
  {productID : 3, weight: 1.5, price: 9},
  {productID : 4, weight: 2.5, price: 3},
  {productID : 5, weight: 7.5, price: 1}
];

And I want to remove items based on an array of productID, like below:

$scope.deleteList = [1,3];

Now I try to use two loops to check if the productID if each product is the same with any productID in the deleteList.

angular.forEach($scope.listOfItems , function(value, key){
    var tmp_productID = value.productID ;
    var index        = key;
    angular.forEach($scope.deleteList, function(value,key){
        if(tmp_productID === value){
            $scope.listOfItems .splice(index ,1);
        }
    });
}); 
console.log($scope.listOfItems);

The problem of this is, after it deletes the one with index1, the index3 will be index2, thus it will delete product2 and product5 instead of product2 and product4, which is wrong. Also the performance will be bad if I have a large number of objects in the array and I want to delete half of them. Any idea that I can delete the right record and improve the performance if possible?

Working jsfiddle http://jsfiddle.net/Lvc0u55v/10726/

7
  • Do you have any problem with creating a new array rather than modifying in place? Commented Oct 12, 2016 at 15:14
  • Click the <> and create a minimal reproducible example Commented Oct 12, 2016 at 15:15
  • I prefer not to create an new array, because my html has data bundling with array listOfItems Commented Oct 12, 2016 at 15:16
  • How about just a regular for loop in reverse? Commented Oct 12, 2016 at 15:17
  • @adeneo I believe it will the same result, because the problem comes from the index in the splice function Commented Oct 12, 2016 at 15:18

1 Answer 1

1

You could iterate in reverse, that way the splicing won't affect the previous indices

var $scope = {};

$scope.listOfItems = [{
    productID: 1,
    weight: 3.5,
    price: 5
}, {
    productID: 2,
    weight: 4.5,
    price: 8
}, {
    productID: 3,
    weight: 1.5,
    price: 9
}, {
    productID: 4,
    weight: 2.5,
    price: 3
}, {
    productID: 5,
    weight: 7.5,
    price: 1
}];

$scope.deleteList = [1, 3];

for ( var i = $scope.listOfItems.length; i--; ) {
    if ( $scope.deleteList.indexOf( $scope.listOfItems[i].productID ) !== -1 ) {
    	$scope.listOfItems.splice(i ,1);
    }
}

console.log($scope.listOfItems);
.as-console-wrapper {top: 0; max-height: 100%!important}

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

3 Comments

This has the same problem, please check my updated jsfiddle with your function
@Jaaaaaaay - isn't it working just fine? You know you're checking the productID, not the index, so [1,4] removes the objects with those productID 's ?
If you want the index instead, that's easy -> jsfiddle.net/adeneo/Lvc0u55v/10725

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.