Trying to count the checked items !?

I am trying to count the number of checked items in the list created using ionic 2 and TS, i appreciate any help, thanks in advance

html code
<ion-checkbox [checked]="item.checked" (click)="toggleItem(item)" class="checklist-item"></ion-checkbox>
TS code

` toggleItem(item): void {



Why not just make the count in for ?

sorry what do you mean? i am a newbie :frowning:

I don’t know if i understand well your question, but look at

Dear Didelotk, thank you for your support, it is what i need . please look at my code and tell me what i must do else, please …

Thank you in advance.

I used the below code and it worked, thank you for your support

 getItemsCount(): number {
        let count= 0;
        this.checklist.items.forEach((item) => {
        return count;


looks like your checklist is a class instance with methods like toggleItem, renameItem etc. Therefore I’d suggest to implement counting the checked items there.

That being said, it seems checklist also exposes the raw list of items as property. Counting the checked ones seems straightforward then:

var count = this.checklist.items.filter(item => item.checked).length;

I hope I understood your question correctly.

Edit: Ok, I was distracted while writing that, so it took me a while during which you already figured it out :slight_smile:

1 Like

I would rather use filter instead of a foreach.

Something like that:

var count = this.checklist.items.filter((item) => item.checked).length;
1 Like

Little change with your code

if i may ask, why would you rather using filter than forEach ?

cleaner code… and if it exist why not use it? You might see performance issue with large scale arrays. But if its anything around 500 and less filter is the way to go IMO.