How do I make a list from a list of toggled items?


#1

Pretty newbie question, I know: I have an array of items with toggles that are displayed on the page and once I click a button I want to make an array of those toggled items - so I can use them to run another function.

I can get the checked value without issue, but how do I properly iterate through the array ( without using something like index and array length, which seems a bit archaic here )?


#2

Can you share a bit more of what you’ve tried so far?


#3

Sure.

This is each item in the list, in the html:

<ion-item *ngFor="let item of itemmodel.items">
    <ion-label>{{ item.name }}</ion-label>
    <ion-toggle checked="{{ item.checked}}"></ion-toggle>
</ion-item>

Itemmodel has an items array and each item in it has a text field ( name ) and a boolean field ( checked ).

And this is the function pressing the button performs:

var i=0;
var size=this.itemmodel.items.length;
for(i=0;i<size;i++) {
    if (this.itemmodel.items[i].checked == true) {
        //do stuff
    }
}