Atul Sharma
Atul Sharma

Reputation: 10655

Remove element from Angular Array

I have a angular array of 5 elements.

$scope.details;

and $scope.details have these following elements [id, name, points].

and now using loop I have added a new element to this $scope.details for every element.

for(var i = 0; i < $scope.details.length; i++){
  $scope.details[i].check = 'f';
}

So, the elements are [id, name, points, check];

Now, after performing the logic i need to remove the check from all 5 elements of $scope.details.

Upvotes: 1

Views: 243

Answers (3)

MiPhyo
MiPhyo

Reputation: 49

We can pass index / id and write delete function in service js file as below:

RemoveFromCart(indexP){
    this.items.splice(indexP, 1);   
  }

Upvotes: 0

Andriy Ivaneyko
Andriy Ivaneyko

Reputation: 22021

Well, your code aren't adding check property to each element in details Array. To do it update code to:

for(var i=0; i<$scope.details.length; i++){
  $scope.details[i].check = 'f';
}

To remove check element from each item use for loop and delete:

for(var i=0; i<$scope.details.length, i++){
  delete $scope.details[i].check
}

In your code snippet you adding check property to $scope.details, so you can remove it without for loop:

delete $scope.details.check

Upvotes: 2

miensol
miensol

Reputation: 41608

You can use delete like so:

for(var i=0;i<$scope.details.length;i++){
  delete $scope.details[i].check;
}

Or using slightly newer API

$scope.details.forEach(function(item){
  delete item.check;
}

Upvotes: 2

Related Questions