1

Here is my sample data

    this.factorList = [ 
    {      
    factorEnrollment: "OPTIONAL",
    factorProvider: "OKTA",
    factorStatus: "NOT_SETUP",
    factorType: "call"
  },
    {  
    factorEnrollment: "OPTIONAL",
    factorProvider: "OKTA",
    factorStatus: "NOT_SETUP",
    factorType: "sms"
  },
  {  
    factorEnrollment: "REQUIRED",
    factorProvider: "OKTA",
    factorStatus: "NOT_SETUP",
    factorType: "sms"
  }
    ];

How to get a count of how many FactorEnrollment: "REQUIRED" is in the array above in angular 8?

1
  • I would say, it is not related to the Angular but you can use something like this : this.factorList.filter(d => d.factorEnrollment === "REQUIRED").length Commented Apr 2, 2020 at 18:38

5 Answers 5

1

You could filter the array by the required condition and get the length of that array.

const factorList = [ 
    {      
    factorEnrollment: "OPTIONAL",
    factorProvider: "OKTA",
    factorStatus: "NOT_SETUP",
    factorType: "call"
  },
    {  
    factorEnrollment: "OPTIONAL",
    factorProvider: "OKTA",
    factorStatus: "NOT_SETUP",
    factorType: "sms"
  },
  {  
    factorEnrollment: "REQUIRED",
    factorProvider: "OKTA",
    factorStatus: "NOT_SETUP",
    factorType: "sms"
  }
    ];
    
 
 const filtered = factorList.filter(x => x.factorEnrollment === 'REQUIRED');
 
 console.log('Number of required:', filtered.length);

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

Comments

0

Try like this:

let count = this.factorList.filter(x => x.factorEnrollment == "REQUIRED").length

Working Demo

Comments

0

You can use filter method like

 let count = this.factorList.filter(x => x.factorEnrollment === 'REQUIRED').length;

Comments

0

@Margerine

You can use the array methods of 'filter'

`factorList.filter(factor => factor.factorEnrollment === 'REQUIRED')`

Comments

0

you can use the javascript array.filter operator.

let matches: number = this.factoryList.filter( item => item.factorEnrollment === 'REQUIRED').length

that will return the count of how many items match your criteria

you could also use reduce operator if you'd like

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.