javascript - Angularjs get indexOf through filter - Stack Overflow

I have a set of JSON data returned by the backend API, and i need to loop through existing array and ge

I have a set of JSON data returned by the backend API, and i need to loop through existing array and get the index to be used in splice, thus i am using indexOf method bined with the filter function from angular.

I am able to filter the data out from existing array, however i am unable to get the index of the array, it returned -1.

This is how do it.

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);
})

I have a set of JSON data returned by the backend API, and i need to loop through existing array and get the index to be used in splice, thus i am using indexOf method bined with the filter function from angular.

I am able to filter the data out from existing array, however i am unable to get the index of the array, it returned -1.

This is how do it.

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);
})
Share Improve this question asked May 18, 2016 at 5:51 JosephJoseph 7335 gold badges18 silver badges42 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 3

$filter returns an array, from which you need to get the first element, and then search with indexOf:

var index = $scope.Tablelist.indexOf(
                $filter('filter')($scope.Tablelist,{id: $scope.data.id},true)[0]
            );

However, I would just use Array.prototype.findIndex(). It will be much faster, since it won't iterate the Tablelist more than it needs to.

var index = $scope.Tablelist.findIndex(function(item) {
    return item.id === $scope.data.id;
});

Or a regular for loop if you want better browser patibility:

var index = -1;
for (var i = 0; i < $scope.Tablelist.length; i++)
    if ($scope.Tablelist[i].id === $scope.data.id) {
        index = i;
        break;
    }
}

Try this:

var object = $filter('filter')($scope.Tablelist, function (d) {
   return d.id === $scope.data.id;
})[0]
console.log(object);
console.log($scope.Tablelist.indexOf(object);
$filter('filter')($scope.Tablelist,{id: $scope.data.id},true) 

Returns a an array of length 1, not the item itself.

$filter('filter')($scope.Tablelist,{id: $scope.data.id},true)[0] 

may do the trick

$filter returns an array, you are trying to find index of that array in your Tablelist which returns -1, try:

var index =
  $scope.Tablelist.indexOf($filter('filter')($scope.Tablelist,{id: $scope.data.id},true)[0]));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744954609a4603119.html

相关推荐

  • javascript - Angularjs get indexOf through filter - Stack Overflow

    I have a set of JSON data returned by the backend API, and i need to loop through existing array and ge

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信