Search code examples
javascripttypescriptangular7angular-directive

How to check value exist on column Type on array of object report Control?


I need to check column Type value but it not catch or give me message column type exist ON console log

ReportControl: any[] = []
this.ReportControl

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

I need to check columnType=2 so I write

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

it does not catch message console log column type exists

Why what is wrong and How to solve that?


Solution

  • As I'm seeing from the provided code, ReportControl is an array of objects not a simple array and not a simple object. So you need to iterate through that array and then check if columnType exists and check its value.

    For example you can do the following:

    1/ Start by iterating the ReportControl array:

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

    2/ Do you check inside the map method:

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

    So the full code will be:

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

    There are multiple methods which you can use to achieve this behavior but I think this is the simplest.