Total Pageviews

2018/04/10

[JavaScript] Remove an item from array

Scenario
Assume I have an array of data in $scope.tab2.sentence.entities
I would like to remove an item from array based on the value which I provide, how to do it?


How-To

You can utilize splice to fulfill the requirement.
Here has the code snippet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 $scope.removeTab2Entity = function(entity) {
      var index;
      for (var i = 0; i < $scope.tab2.sentence.entities.length; i++) {
          if ($scope.tab2.sentence.entities[i].value == entity.value) {
              index = i;
              break;
          }
      }
      $scope.tab2.sentence.entities.splice(index, 1);
  }

splice syntax and its parameter information are as bellows:

Reference
[1] https://www.w3schools.com/jsref/jsref_splice.asp

No comments: