Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs check if passing value exists in array

I'm trying to pass two text field values into and array using AngularJS, but I also want to check if the first value being passed already exists in the array. If it does I do not want it added again, if it doesn't exist then add the values. I can get it to add the values but I'm getting stuck trying to check if the last name value exists already in the array. I had found some examples I tried to follow but I cant seem to get them to work. Thanks for any help!

<div class="panel-body">
    <div ng-controller="TodoCtrl">
        <ul>
            <li li ng-repeat="todo in todos">
            {{todo.text}}, {{todo.name}}
            </li>
        </ul>
        <form class="form-horizontal">
        <input type="text" ng-model="formTodoLast" ng-model-instant>
        <input type="text" ng-model="formTodoFirst" ng-model-instant>
        <button class="btn" ng-click="addTodo()"><i class="icon-plus"></i>Add</button>
    </div>
</div>

todo.js

function TodoCtrl($scope) { 
    $scope.todos = [];

    $scope.addTodo = function () {

    if($scope.todos.indexOf(text:$scope.formTodoLast) == -1) {
        $scope.todos.push({text:$scope.formTodoLast, name:$scope.formTodoFirst});
        $scope.formTodoText = ' ';
        $scope.formTodoName = ' ';
        }
    }
}
like image 537
Trevor Avatar asked Mar 04 '14 16:03

Trevor


People also ask

How do you check if an array contains a value in AngularJS?

isArray() Function in AngularJS is used to return TRUE if the reference is an array and FALSE if it is not an array. Syntax: angular. isArray(value);

How do you check if an array contains a value?

For primitive values, use the array. includes() method to check if an array contains a value. For objects, use the isEqual() helper function to compare objects and array. some() method to check if the array contains the object.

How do you check whether a value is present in an array of objects in JavaScript?

JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.


3 Answers

As Craig said, you need to loop through the array as such

var addToArray=true;
for(var i=0;i<$scope.todos.length;i++){
    if($scope.todos[i].text===$scope.formTodoLast){
        addToArray=false;
    }
}
if(addToArray){
    $scope.todos.push({text:$scope.formTodoLast, name:$scope.formTodoFirst});
}
$scope.formTodoText = ' ';
$scope.formTodoName = ' ';
like image 97
NicolasMoise Avatar answered Oct 20 '22 04:10

NicolasMoise


This "$scope.todos.indexOf(text:$scope.formTodoLast)" won't work. You have to loop through the array and check the formTodoLast property of each object. Or, if using full jQuery, you could use grep().

Here's an example, note the comparison is case sensitive right now. If it's a long list, consider using a Javascript for loop instead of Angular's forEach, you can break out of the for loop and it's likely faster.

function TodoCtrl($scope) { 
    $scope.todos = [];

    $scope.addTodo = function () {

        var missingName = true;
        angular.forEach($scope.todos, function(value, key){
           if(value.formTodoLast == $scope.formTodoLast)
           {
               missingName = false;
           }
         });

        if(missingName) {
            $scope.todos.push({text:$scope.formTodoLast, name:$scope.formTodoFirst});
            $scope.formTodoText = ' ';
            $scope.formTodoName = ' ';
        }
    }
}
like image 23
Craig Squire Avatar answered Oct 20 '22 05:10

Craig Squire


Seems there is syntax error in your snippet. If you prefer one line version on if-statement, change your if condition into:

function TodoCtrl($scope) { 
    $scope.todos = [];

    $scope.addTodo = function () {

    if(!$scope.todos.some(function(td){return td.text===$scope.formTodoLast})) {
        $scope.todos.push({text:$scope.formTodoLast, name:$scope.formTodoFirst});
        $scope.formTodoText = ' ';
        $scope.formTodoName = ' ';
        }
    };
}

Array.prototype.some returns true if any of the element meet the requirement

some() executes the callback function once for each element present in the array until it finds one where callback returns a true value. If such an element is found, some() immediately returns true. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some

like image 45
ssnau Avatar answered Oct 20 '22 05:10

ssnau