1

I have 2 list. One bigger and one smaller. I have added infinite scroll to keep adding items from bigger list to smaller one.

// Bigger list (data from DB)
var data; 
// Smaller list 
$scope.projects = [];

$scope.$watch('projectSearch', function (val) {
    if (typeof val !== 'undefined' && val !== "") {
        //console.log("SEARCH: " + val);
        for (var a = 0; a < data.length; a++) {
            if (data[a].name.toLowerCase().indexOf(val) > -1) {
                console.log("FOUND: " + data[a].name);
                if($scope.projects.indexOf(data[a].name) === -1) {
                    console.log("PUSHED " + data[a].name);
                    $scope.projects.push( data[a]);

                } else {
                    console.log("ALREADY IN " + data[a].name);
                }
            }
        }
    }  
});

Search form:

<input id="search" ng-model="projectSearch" placeholder="Search ..."/>

The problem is it keeps pushing items to new list. It does not check this if statement correctly

if($scope.projects.indexOf(data[a].name) === -1) {
    console.log("PUSHED " + data[a].name);
    $scope.projects.push( data[a]);
}
2
  • You are not pushing the name. $scope.projects.push(data[a].name); Commented Jun 6, 2016 at 8:09
  • I push object, this one works fine :) Commented Jun 6, 2016 at 8:10

2 Answers 2

3

you should check for data[a], not its property name

if($scope.projects.indexOf(data[a]) === -1) {
    console.log("PUSHED " + data[a].name);
    $scope.projects.push( data[a]);
}

also, you could remove data[a] after copy to make next search faster

Sign up to request clarification or add additional context in comments.

Comments

1

$scope.projects should be type of string assuming data[a].name is string then your indexOf function properly

1 Comment

I push only objects which contain characters from input field. I do not check if it's string.

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.