xzk
xzk

Reputation: 877

Typescript String equality check not working

I am using Angular 5.

In my component, I check if value is equal to "DATE".

isDateColumn(columnName: string){
    this.configData.some((el) => {
      if (columnName == el.key.columnName){
        console.log("FOUND: " + el.dataType + " For " + columnName);
        return el.dataType === "DATE";
      }
    })
    return false;
  }

In my console, I found something like below:

FOUND: DATE For CLIENT_START_DT

But this function still returns me false.

Why is it so?

Upvotes: 1

Views: 2649

Answers (1)

Saksham Gupta
Saksham Gupta

Reputation: 628

This is happening because your return statement return el.dataType === "DATE"; returns from the array.some function not the isDateColumn function. So in the end return false; executes in all cases.

This works:

isDateColumn(columnName: string){
let found = false;
this.configData.some((el) => {
  if (columnName == el.key.columnName && el.dataType === "DATE"){
    console.log("FOUND: " + el.dataType + " For " + columnName);
    found = true;
  }
})
return found;
}

Upvotes: 3

Related Questions