简体   繁体   中英

Check input value in array

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"}]

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

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="";
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM