如何检查对象报表控件数组的列类型上是否存在值?

问题描述 投票:0回答:3

我需要检查列类型值,但是它无法捕获或向我发送消息,列类型在控制台日志中存在

ReportControl: any[] = []
this.ReportControl

value of ReportControl is
[
    {
        "reportId": 2028,
        "fieldName": "offilneURL",
        "reportStatus": "HiddenColumn",
        "columnType": 1
    },
    {
        "reportId": 2028,
        "fieldName": "onlineUrl",
        "reportStatus": null,
        "columnType": 2
    }]

我需要检查columnType = 2所以我写

if (this.ReportControl["columnType"] == 2) {
    console.log("column type exist");
}

它不捕获消息控制台日志列类型存在

为什么错了以及如何解决?

javascript typescript angular7 angular-directive
3个回答
0
投票

由于具有对象数组,因此需要检查每个对象的值,如下所示。

var rc = [
    {
        "reportId": 2028,
        "fieldName": "offilneURL",
        "reportStatus": "HiddenColumn",
        "columnType": 1
    },
    {
        "reportId": 2028,
        "fieldName": "onlineUrl",
        "reportStatus": null,
        "columnType": 2
    }
];

var isValueexistt = false;
for (let i = 0; i < rc.length; i++) {
    var obj = rc[i];
    isValueexistt = obj["columnType"] == 2;
    if (isValueexistt) {
        break;
    }
}

console.log(isValueexistt);

0
投票

由于是数组,因此必须遍历元素

var reportControl = [{
    "reportId": 2028,
    "fieldName": "offilneURL",
    "reportStatus": "HiddenColumn",
    "columnType": 1
  },
  {
    "reportId": 2028,
    "fieldName": "onlineUrl",
    "reportStatus": null,
    "columnType": 2
  }
]

let found = false;
for (let i = 0; i < reportControl.length; i++) {
   if (reportControl[i]['columnType'] === '2') {
       found = true;
       break;
   }
}

if (found) {
   console.log("column type exist");
}

请注意,这是纯JavaScript。由于您使用的是角度,因此必须将reportControl替换为this.ReportControl


0
投票

我从提供的代码中看到,ReportControl是对象的数组,不是简单的数组,也不是简单的对象。因此,您需要遍历该数组,然后检查columnType是否存在并检查其值。

例如,您可以执行以下操作:

1 /首先迭代ReportControl数组:

this.ReportControl.map((reportElement) => {

});

2 /您是否在地图方法内检查过:

if("columnType" in reportElement && reportElement["columnType"] == 2) {
    console.log("column type exist");
}

所以完整的代码将是:

this.ReportControl.map((reportElement) => {
    if("columnType" in reportElement && reportElement["columnType"] == 2) {
        console.log("column type exist");
    }
});

您可以使用多种方法来实现此行为,但我认为这是最简单的。

© www.soinside.com 2019 - 2024. All rights reserved.