js判断数组是否包含指定元素的方法

通过prototype定义了数组方法,这样就可以在任意数组调用contains方法

Array.prototype.contains = function ( needle ) {
  for (i in this) {
    if (this[i] == needle) return true;
  }
  return false;
}

例子:

var x = Array();
if (x.contains('foo')) {
  // do something special
}

angularjs运用:

$watch监控"dataList.car_type"的值变化
    $scope.carList = false;
    $scope.$watch("dataList.car_type", function (newValue, oldValue) 
    {
        if ($scope.dataList.car_type.contains('car') ) {
            $scope.carList = true;
        }
       
        if($scope.dataList.car_type.length !=0) {
            $(".personInput").css("color","#9e9e9e");
            $(".personInput input").attr("disabled","disabled");
        }else {
            $(".personInput").css("color","#444444");
            $(".personInput input").removeAttr("disabled");
        }
    },true);

 

posted @ 2018-04-03 16:25  miny_simp  阅读(514)  评论(0编辑  收藏  举报