简体   繁体   English

使用 Boolean 条件循环通过两个 JSON Arrays [角度 10]

[英]Loop Through Two JSON Arrays With a Boolean Condition [Angular 10]

I have two JSON arrays from an external that I want to loop in Angular, but I am unsure how to do it.我有两个来自外部的 JSON arrays 我想在 Angular 中循环,但我不确定该怎么做。

The first array in the object looks like ViewAttributes.FunctionNames and the second array is ViewAttributes.FunctionActive. object 中的第一个数组看起来像 ViewAttributes.FunctionNames,第二个数组是 ViewAttributes.FunctionActive。

Right now I am using a right menu but my objective is to show the correct FunctionNames value in the menu options when the boolean position for each value is true, not false .现在我正在使用正确的菜单,但我的目标是在 boolean position 每个值为true 而不是 false时在菜单选项中显示正确的 FunctionNames 值。 What do I need to change?我需要改变什么?

my code is below for example:例如,我的代码如下:

view.component.html view.component.html

  <mat-menu #contextMenu="matMenu" #contextMenu2="matMenu">
    <ng-template matMenuContent let-action="action">
      <button mat-menu-item (click)="onContextMenuAction(action)">{{FunctionNames}}</button>
    </ng-template>
  </mat-menu>

view.component.ts view.component.ts

      // Right Click Context Menu for View Data Table

    onContextMenu(event: MouseEvent, action: ViewDataSource) {
      event.preventDefault();
      this.contextMenuPosition.x = event.clientX + 'px';
      this.contextMenuPosition.y = event.clientY + 'px';
      this.contextMenu.menuData = { action: action };
      this.contextMenu.menu.focusFirstItem('mouse');
      this.contextMenu.openMenu();
      console.log(this.FunctionNames);
    }

    onContextMenuAction(action: ViewDataSource ) {
      console.log(action);
      console.log(parseInt(action[0]));
      console.log(parseInt(action[3]));

      // this.launchService.launchAction(this.line.targetActionTag.value, this.line.targetActionType);
      // tslint:disable-next-line: radix
      this.launchService.launchAction(parseInt(action[0]), parseInt(action[3]));
    //   debugger;

    }

and my two JSON array object looks like:我的两个 JSON 阵列 object 看起来像:

ViewAttributes.FunctionNames = [Value1, Value2, Value3, ....]

ViewAttributes.FunctionActive = [true,false,true, ...]

If you want to show the FunctionNames based on the boolean value at the corresponding index in FunctionActive array.Then below code might help you如果您想在FunctionActive数组中的相应索引处显示基于 boolean 值的FunctionNames 。那么下面的代码可能会对您有所帮助

ViewAttributes.FunctionNames =ViewAttributes.FunctionNames.filter((each,index)=>{return ViewAttributes.FunctionActive [index]})

This will filter the array based on the boolean value in FunctionActive array.这将根据FunctionActive数组中的 boolean 值过滤数组。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM