javascript - 检查数组中的输入值

标签 javascript arrays json angularjs

我有 json 数组,我想检查输入字段的值是否存在于数组中。

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

我的数组是employeeData.seat_array

如何使用 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");
        }
    });

});

数组结构:

[{"seat_number":"834"},{"seat_number":"8F3"},{"seat_number":"891"}]

最佳答案

只需使用 Array.prototype.indexOf :

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

我不确定 employeeData.seat_array 的结构,但如果它是对象数组,您可以使用 Array.prototype.some而不是indexOf:

var inArray = $scope.employeeData.seat_array.some(function(obj) {
    return obj.seat_number === $scope.employeeData.new_seat_number;
});

关于javascript - 检查数组中的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30210516/

相关文章:

javascript - 我如何在 Firebase Angularjs 中使用 $id 作为数字或新变量?

代码隐藏语法中的 javascript

javascript - 获取从二月开始的年度季度编号

javascript - 有没有一种方法可以在 jQuery 中克隆数组?

c++ - 复制数组并清理

python - 为什么使用使用 utf-8 编码的 Windows txt 文件时会出现 Json 的 ValueError 错误?

python - Flask-Restful - 返回带有嵌套数组的 json

javascript - Nodejs 代码未按预期执行

javascript - 使用数组创建多项选择测验

javascript - 简单的ajax请求加载外部json文件