简体   繁体   English

如何检查数组中对象列表的至少一个值在Angular中是否为真?

[英]How to check if at least one value of a list of objects in an array is true in Angular?

In my website I've got an array which also contains another array with some values: 在我的网站上,我有一个数组,其中还包含另一个带有一些值的数组:

[
    {
        "name": "Pete",
        "place": "HOME",
        "houses": [
            {
                "key": "OFFICE",
                "value": false
            },
            {
                "key": "HOME",
                "value": true
            },
            {
                "key": "SHOP",
                "value": true
            }
        ]
    },
    {
        "name": "William",
        "place": "OFFICE",
        "houses": [
            {
                "key": "OFFICE",
                "value": false
            },
            {
                "key": "HOME",
                "value": false
            },
            {
                "key": "SHOP",
                "value": false
            }
        ]
    }
]

I loop over the two objects fine, but I only want to loop over the houses if at least one of the value s of houses is true. 我遍历两个对象很好,但我只是想遍历所有的houses ,如果的至少一个valuehouses是真的。 So for Pete I want to loop over the houses, but not for William. 因此,对于皮特,我想遍历房屋,但对威廉而言。 I currently have this code: 我目前有以下代码:

<div ng-repeat="person in persons">
    {{ person.name }} in {{ person.place }} 
    <div ng-if="WHAT TO PUT HERE TO CHECK IF AT LEAST ONE OF THE VALUES IS TRUE??">
        has got the following houses: 
        <span ng-repeat="house in person.houses">
            <span ng-if="house.value">{{ house.key }} / </span>
        </span>
    </div>
</div>

but I have no clue what to put in the ng-if to check whether any of the value s is true. 但是我不知道在ng-if中输入什么来检查s的value是否为真。 Does anybody know how I should tackle this? 有人知道我该怎么解决吗?

Assign flag to parent object, simple implementation would be like this 将标志分配给父对象,简单的实现就是这样

var people = [ /*your long array ommited*/ ];
angular.forEach(people, function(person){
    //checking length of (get house in person.houses where house.value === true)
    person.has_true_house = person.houses.some(function(item){
        return item.value === true;
    });
});

then in your view 然后在您看来

<div ng-repeat="person in persons">
    {{ person.name }} in {{ person.place }} 
    <div ng-if="person.has_true_house">
        has got the following houses: 
        <span ng-repeat="house in person.houses">
            <span ng-if="house.value">{{ house.key }} / </span>
        </span>
    </div>
</div>

The best way to solve this problem is to filter the array of persons in your controller: 解决此问题的最佳方法是过滤控制器中的人员数组:

app.controller('MainCtrl', function($scope) {

  // Your original data
  $scope.persons = [...];

  // Initialize filtered persons
  $scope.filteredPersons = [];

  // Update filtered persons when persons are updated
  $scope.$watch('persons', function(persons){
    $scope.filteredPersons = filterPersons($scope.persons);
  });

});

// Pure function to filter the data you need
function filterPersons(persons){
  var filteredPersons = [];
  persons.forEach(function(person){
    var truthyHouses = person.houses.filter(function(house){
      return house.value === true;  
    });
    if(truthyHouses.length > 0){
      filteredPersons.push(person);
    }
  });
  return filteredPersons;
}

which will greatly simplify your view because the controller already provides you with the filtered data: 这将大大简化您的视图,因为控制器已经为您提供了过滤后的数据:

<div ng-repeat="person in filteredPersons">
    {{ person.name }} in {{ person.place }} has got the following houses: 
    <ul>
      <li ng-repeat="house in person.houses">
        {{ house.key }}
      </li>
    </ul>
</div>

I have created a working plunk for you here . 我在这里为您制造了一个工作小品。

One way to solve this problem is by using the the filter filter inside the ngIf expression. 解决此问题的一种方法是使用ngIf表达式内的filter过滤器。

DEMO 演示

HTML HTML

<div ng-repeat="person in persons">
  {{ person.name }} in {{ person.place }} 
  <div>
    <span ng-if="houses.length > 0">has got the following houses:</span>
    <span ng-repeat="house in houses = (person.houses | filter: {value: true})">
      <span>{{ house.key }}/</span>
    </span>
  </div>
</div>

You can assign an isolated houses array variable inside the ng-repeat which is filtered with houses that has a true value . 您可以在ng-repeat内分配一个独立的houses数组变量,该变量会被具有true value house过滤掉。 You can use the houses array length as a condition to show the label. 您可以使用houses数组长度作为显示标签的条件。 Lastly, you don't need to check whether each house a value since it has already been filtered. 最后,您不需要检查每个房屋是否都有值,因为它已经过过滤。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Yup 验证对象数组中至少一个 object 键具有真实值? - How to validate with Yup that in array of objects at least one of object keys has true value? 如何检查对象的值是否与 Angular 中数组列表中的任何值匹配 - How to Check If a Value of Objects Matches Any Value in a List of Array in Angular 每个对象在对象数组中都应该至少有一个真实的属性值? - every object should have at least one true property value in array of objects? 删除包含相同键且至少有一个值为 true 的所有对象数组 - Remove all array of objects that contain same key and has at least one value true javascript-如何检查数组是否至少有一个负值? - javascript - How to check if array has at least one negative value? 如何检查数组的至少一项是否包含在 object 的数组中并遍历所有对象(JS) - How to check if at least one item of an array is included in an array of an object and loop through all objects (JS) 如何检查数组是否包含至少一个对象? - How to check if array contains at least one object ? 当至少一个对象的属性与测试的属性匹配时,如何遍历对象数组并返回 true/false object - How can loop over an array of objects and return true/false when at least one of the object's properties matches the properties of a test object 检查反应js中至少一个条件是否为真 - Check if at least one condition is true in react js JavaScript-检查至少一个变量为true - JavaScript - Check if at least one variable is true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM