After receiving a set of JSON data from the backend API, I found myself in need of looping through an existing array to extract the index for use with the splice
method. In order to achieve this, I decided to utilize the combination of the indexOf
method and the filter function provided by Angular.
Although I successfully filtered out the desired data from the array, I encountered difficulty when trying to retrieve the index. Each time I attempted to do so, it resulted in a return of -1
.
Below is a snippet showcasing my approach:
JS
angular.forEach($scope.data, function(){
var index = $scope.Tablelist.indexOf($filter('filter')($scope.Tablelist,{id: $scope.data.id},true));
console.log($filter('filter')($scope.Tablelist,{id: $scope.data.id},true));
console.log(index);
})