0

I am new to TypeScript and working on an Angular project.

I am making an API call and making few operations on the data received:

public data_Config: IConfig[] = [];

this.getService.Data(input).subscribe(
    data => {
        this.data_Config = data;
        this.data_Config.forEach(itm => { 
            if(itm.aFl == 'Y'){
                itm.Levels.push('A')
            }
            if(itm.bFl == 'Y'){
                itm.Levels.push('B')
            }
            if(itm.cFl == 'Y'){
                itm.Levels.push('C')
            }
        });
        this.dataSource_prgmConfiguration = new MatTableDataSource(this.data_prgmConfiguration);
        this.dataSource_prgmConfiguration.paginator = this.paginatorPrgmConfiguration;
        this.dataSource_prgmConfiguration.sort = this.sortPrgmConfiguration;
});

IConfig has several properties including 10-12 flag properties likeaFl,bFl,cFl. Among the Flag properties which ever are true I want to add them to an array. I have done it in a simple way using if condition, but with 11-12 flags as many if conditions will be required, is there any better way to implement this?

Adding IConfig

export interface IConfig {
    TypeNm: string;
    aFl: string;
    bFl: string;
    cFl: string;
    dFl: string;
    eFl: string;
    fFl: string;
    gFl: string;
    hFl: string;
    PlaceHolder: string;
    Association: string;
    ActiveFl: string;
    Actions: string;
    AssociatedProgramsStr?: string;
    Levels?: string[];
}
3
  • 1
    A simpler way would be storing all the flags and it's corresponding data which needs to be stored in a an object {aF1:'A',bF1:'b',...}. Then traverse the above object keys then if item has the key then insert the corresponding value from the object for the flag. Commented Jul 28, 2020 at 5:40
  • Please share the the IConfig definition Commented Jul 28, 2020 at 5:43
  • added IConfig definition Commented Jul 28, 2020 at 5:48

3 Answers 3

1

you can do something like this:

Object.keys(this.dataConfig).filter(key => key.indexOf('Fl') != -1).forEach(flag => {
    if(this.dataConfig[flag] == 'Y')
        itm.Levels.push(this.dataConfig[glag].charAt(0).toUpperCase());
}

this code is valid only if all your flags are with the same time of xFl where x is some letter and this flag refer to the same upper case letter that needs to be push to the Levels array. a bit explanation: first you extract the keys from your object that are flags, then you iterate on them and check your condition.

Sign up to request clarification or add additional context in comments.

1 Comment

I get an error vendor.js:77227 ERROR TypeError: Cannot read property 'push' of undefined .
1

There are multiple ways based on the requirement,

I'd assume IConfig interface has only aF1, bF1, cF1 ... boolean properties, then you could something like following,

Solution : 1

  this.data_Config.forEach(itm => { 

      Object.keys(itm).forEah(key=>{
          if(this.data_Config[key] === 'Y'){
                itm.Levels.push('whatever'); // I don't know exactly what 'A','B' n all represent           }
      })
       
   });

Solution: 2

Let's say if IConfig interface has some other properties eg height, width some non-boolean, in such case you can do following,

const booleanProperties = ['aF1', 'bF1', 'cF1']; // define bool properties in array

this.data_Config.forEach(itm => { 

       Object.keys(itm).forEah(key=>{
           if(booleanProperties.includes(key) && this.data_Config[key] === 'Y'){
                 itm.Levels.push('whatever'); // I don't know exactly what 'A','B' n all represent           }
       })
           
});

2 Comments

I get an error vendor.js:77227 ERROR TypeError: Cannot read property 'push' of undefined .
itm.Levels is it array ? it should be array.
0

In typescript you can create an ENUM like this.

enum enumVal {
  aFl  = 'A',
  bFl  = 'B'
}

Then on iteration check the value and add into your desired result.

result:any[] = [];
this.data_Config.forEach(key=>{
 if(this.data_Config[key] === 'Y'){
    result.push(enumVal[key]); 
 }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.