检查以确保数组中的每个字段都等于一个值

时间:2019-05-21 02:12:41

标签: javascript angular multidimensional-array

我有这样的数据:

 this.registrations =   [{
    event: "Juniors 18s",
    day: "Friday",
    group: "nonpro",
    players: [{
      first: "Mary",
      last: "Mack",
      email: "marymack@dressedinblack.com",
      phone: "8888675309",
      signed: false,
      waivers: [{
        has_signed: true,
        token: "ab",
        url: "somesite.com",
        signatureUrl: "someothersite.com",
        message: "Minor waiver"
        },
        {
        has_signed: true,
        token: "ab",
        url: "somesite.com",
        signatureUrl: "someothersite.com",
        message: "Parental waiver"
        }
      ]
    },
    {... another record like that one}
    ]

是否有一种方法可以显示每个玩家是否签署了所有豁免?我知道有一个.every函数,但是我不确定如何在此嵌套数据结构中使用它。

我在想类似下面的内容,但是没有用:

this.registrations.has_signed = this.registrations.waivers.every( waiver => waiver.has_signed === true ) 

然后我尝试了这个,至少看起来似乎更接近一场胜利(但并不完全)。没有使用players.signed属性,但是默认情况下有人将其放置为false,所以我很想使用它:

  this.registrations.forEach(reg => {
    reg.players.forEach(p => {
      if (p.waivers.every(waiver => waiver.has_signed === true)) {
        p.signed = true;
      } else {
        p.signed = false;
      }
      return reg;
    });
  });

2 个答案:

答案 0 :(得分:0)

此代码段应完成所需的任务。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  <table>
    <th ng-repeat="year in yearList" ng-if="$index == 0"> {{year.years}}</th>
    <th ng-repeat="year in yearList" ng-if="$index == 1"> {{year.years}}</th>


    <tr ng-repeat="data in dataList">
      <td>{{data.reportTypeDetail}}</td>
      <td>{{data.reportFormula}}</td>
    </tr>
  </table>
</div>

答案 1 :(得分:0)

this.registrations.has_signed = false;
this.registrations.forEach(registration => 
    registration.players.forEach(player => 
        this.registrations.has_signed = player.waivers.every(waiver => waiver.has_signed)
    )
);

您必须首先循环外部数组才能获得豁免,并使用every来找出has-signed