AngularJs - 检查数组对象中是否存在值

时间:2016-11-09 07:43:09

标签: javascript angularjs arrays javascript-objects

if ($scope.array.indexOf(SelectedOptionId) === -1) {console.log('already exists')}

有没有办法检查那种数组对象中是否存在值。我正在使用Angular和下划线。

我已经尝试了所有这些 -

console.log($scope.array.hasOwnProperty(SelectedOptionId)); //returns false

console.log(_.has($scope.array, SelectedOptionId)); //returns false 

childNodes

8 个答案:

答案 0 :(得分:3)

您可以使用Array#some并与in运营商核对。

exists = $scope.array.some(function (o) {
    return SelectedOptionId in o;
});

答案 1 :(得分:0)

检查

function checkExists (type) {
   return $scope.array.some(function (obj) { 
    return obj === type;
  }
}

var chkval=checkExists("your value")

答案 2 :(得分:0)

您可以使用filter。以下代码应返回具有匹配结果的输出数组(如果存在),否则返回空数组:

var array = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}];
var SelectedOptionId = 957;

var result = array.filter(
  function(item) {return item[SelectedOptionId]}
)


console.log(result);

对于您的输入,它返回:

[ { '957': '1269' }, { '957': '1269' } ]

答案 3 :(得分:0)

试试这个:

if($scope.array[SelectedOptionId] || _.includes(_.values($scope.array, SelectedOptionId))) { }

这应该涵盖一个键和一个值。

答案 4 :(得分:0)

custom.js

答案 5 :(得分:0)

$(document).ready(function (){
    if(check2.checked){
        $('#firstDropdown').addClass('myHide');
        $('#secondDropdown').addClass('myVisible');
        $('#thirdDropdown').addClass('myHide');
    }
    if(check3.checked){
        $('#firstDropdown').addClass('myHide');
        $('#secondDropdown').addClass('myHide');
        $('#thirdDropdown').addClass('myVisible');
    }
});

使用下划线

答案 6 :(得分:0)

You can do it using the in operator or the hasOwnProperty function, to check for the existence of a key in an object inside the given array.

The way you've tried using hasOwnProperty function didn't work because you were checking it directly on the array instead of checking against the items in the array.

Check the below code snippet.

angular
  .module('demo', [])
  .controller('HomeController', DefaultController);

function DefaultController() {
  var vm = this;
  vm.items = [{
    "957": "1269"
  }, {
    "958": "1265"
  }, {
    "956": "1259"
  }, {
    "957": "1269"
  }, {
    "947": "1267"
  }];

  var key = '957';
  var isExists = keyExists(key, vm.items);
  console.log('is ' + key + ' exists: ' + isExists);

  function keyExists(key, items) {
    for (var i = 0; i < items.length; i++) {
      // if (key in items[i]) {
      if (items[i].hasOwnProperty(key)) {
        return true;
      }
    }

    return false;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
  <div ng-controller="HomeController as home">
    {{home.items | json}}
  </div>
</div>

答案 7 :(得分:0)

执行此操作的不同方法:

  1. 使用对象hasOwnProperty()方法。
  2. 工作演示:

    &#13;
    &#13;
    var SelectedOptionId = 957;
    
    var arrayObj = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}];
    
    function checkOption(key) {
      for(var i in arrayObj) {
        if(arrayObj[i].hasOwnProperty(key) == true) {
          return key+" exists.";
        } else {
          return key+" Not exists.";
        }
      } 
    };
    
    console.log(checkOption(SelectedOptionId)); // 957 exists.
    &#13;
    &#13;
    &#13;

    1. 使用Array filter()方法。
    2. 工作演示:

      &#13;
      &#13;
      var SelectedOptionId = 957;
      
      var arrayObj = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}];
      
      var result = arrayObj.filter(function(elem) {
        return elem[SelectedOptionId]
      });
      
      if(result == '') {
      console.log(SelectedOptionId+" not exists.");
      } else {
      console.log(SelectedOptionId+" exists.");
      }
      &#13;
      &#13;
      &#13;

      1. 使用some()建议的数组Nina Scholz方法。
      2. 工作演示:

        &#13;
        &#13;
        var SelectedOptionId = 957;
        
        var arrayObj = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}];
        
        var result = arrayObj.some(function (o) {
            return SelectedOptionId in o;
        });
        
        if(result == '') {
        console.log(SelectedOptionId+" not exists.");
        } else {
        console.log(SelectedOptionId+" exists.");
        }
        &#13;
        &#13;
        &#13;