2

I have json array and I want to check the value of input filed is present or not in array.

<input type='text'id='seat_number' name='seat_number' ng-model="employeeData.new_seat_number">

My array is employeeData.seat_array.

How to do it with Angular?

$scope.$watch('employeeData.new_seat_number', function (newVal) {
    console.log(newVal);

    $.each(employeeData, function (i, obj) {
        if (obj.seat_number === $scope.employeeData.new_seat_number) {
            alert("asd");
        }
    });

});

Array structure:

[{"seat_number":"834"},{"seat_number":"8F3"},{"seat_number":"891"}]
1
  • you most cases you not need in jquery when use angular Commented May 13, 2015 at 9:19

2 Answers 2

2

Just use Array.prototype.indexOf:

var inArray = $scope.employeeData.seat_array.indexOf($scope.employeeData.new_seat_number) > -1;

I'm not sure about the structure of the employeeData.seat_array but if it's array of objects you could use Array.prototype.some instead of indexOf:

var inArray = $scope.employeeData.seat_array.some(function(obj) {
    return obj.seat_number === $scope.employeeData.new_seat_number;
});
Sign up to request clarification or add additional context in comments.

3 Comments

seems like it not work, because in seat_array objects instead seat_numbers
Makes sense, in this case some might do the job.
Updates array structer @dfsq
1

I found easy solution

$scope.checkc = function(){


        if(($scope.employeeData.seat_array.indexOf($scope.employeeData.new_seat_number))>-1)
        {
            alert('New seat number not available');
            $scope.employeeData.new_seat_number="";
        }

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.