2

I have this massive

$scope.arrVaritions = [
    [1, 2, 3], 
    [4, 5, 6], 
    [7, 8, 9], 
    [1, 4, 7], 
    [2, 5, 8], 
    [3, 6, 9], 
    [1, 5, 9], 
    [3, 5, 7]
];

How can I find an element index in this array? I'm use this code, but it does not work:

$scope.elementId = 1;    
$filter('filter')($scope.arrVaritions, { $scope.elementId });
2
  • yep not work. SyntaxError: Unexpected token "." Commented Mar 12, 2017 at 10:29
  • $filter('filter')($scope.arrVaritions, { id: $scope.elementId }); - it works, but i have not "ID" Commented Mar 12, 2017 at 10:30

2 Answers 2

2

Try filter it in the right way like in this demo fiddle:

View

<div ng-controller="MyCtrl">
  {{filtered}}
</div>

AngularJS application

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope, $filter) {
    $scope.arrVaritions = [
        [1, 2, 3], 
        [4, 5, 6], 
        [7, 8, 9], 
        [1, 4, 7], 
        [2, 5, 8], 
        [3, 6, 9], 
        [1, 5, 9], 
        [3, 5, 7]
    ];

    $scope.elementId = 1;
    $scope.filtered = $filter('filter')($scope.arrVaritions, $scope.elementId, true);
});

If you want to filter not the exact value just remove the 3rd true option:

$scope.filtered = $filter('filter')($scope.arrVaritions, $scope.elementId);
Sign up to request clarification or add additional context in comments.

3 Comments

Exact same time :) 2017-03-12 10:32:10Z
Yes, I was laughing loud in that moment. =)
upvoted! but I covered corner case :) which I faced usually while working on projects :)
1

It should have only $scope.elementId instead of object, and 3rd option true to match exact value. Otherwise without true if elementId will consider 10, 11, 100, etc. values (it will do contains check).

$filter('filter')($scope.arrVaritions, $scope.elementId, true);

Comments

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.