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

AngularJs - check if value exists in array object

本文关键字:是否 存在 对象 检查 数组 AngularJs      更新时间:2023-09-26
var SelectedOptionId = 957;
$scope.array = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}]

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

我都试过了——

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

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

console.log(_.has($scope.array, SelectedOptionId)); //returns false 
您可以使用

Array#some并与in运算符进行检查。

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

检查这个

function checkExists (type) {
   return $scope.array.some(function (obj) { 
    return obj === type;
  }
}
var chkval=checkExists("your value")

试试这个:

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

这应该涵盖键和值。

let selectedOptionId = "957";
let array = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}];
let filtered = array.filter(function(element){
    return Object.keys(element)[0] === selectedOptionId; 
  });
console.log(filtered);

console.log(_.some($scope.array, function(o) { return _.has(o, "957"); }));

使用下划线

你可以为此使用过滤器。下面的代码应该返回带有匹配结果的输出数组,如果存在,否则它将返回一个空数组:

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' } ]
您可以使用

in 运算符或 hasOwnProperty 函数来检查给定数组内对象中是否存在键。

您尝试使用hasOwnProperty函数的方式不起作用,因为您直接在数组上检查它,而不是检查数组中的项目。

检查下面的代码片段。

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>

不同的方法:

  1. 使用 Object hasOwnProperty(( 方法。

工作演示:

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.

  1. 使用 Array filter(( 方法。

工作演示:

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

  1. 使用 Nina Scholz 建议的 Array some(( 方法。

工作演示:

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