Khushi
Khushi

Reputation: 1859

How to loop inside array and assign to a variable

In my angular2 application which contains checkboxes; the checkbox keys are stored in an array named "ticketselected" and the selected check boxes keys are stored in "selectedArray". I want to assign these keys one by one to a variable and use that variable. I need to splice an array. How can I implement this?

what I have done is:

component.ts

this.ticketSelected
    .map((entry, key) => {
         if (entry == true) {
             selectedArray.push(key);
            }
     })

 var index =   // This should be key
 allTickets.splice(index, 1);

'selectedArray' contains all selected keys.

How should I implement this?

EDIT

 deleteRequest
      .subscribe((data: any) => { // this data contain deleted data ie like {count:1}
                this.ticketSelected
                     .map((entry, key) => {
                      if (entry === true) {
                                selectedArray.push(key);
                       }
                 })

                 selectedArray.forEach((item) => {
                        var index = allTickets.indexOf(item);
                        console.log("index", index);
                        if (index != -1){
                            allTickets.splice(index, 1);
                    }
                    });
                })

Upvotes: 0

Views: 84

Answers (1)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48407

You should use indexOf method.

allTickets=allTickets.filter(function(item){
     return selectedArray.indexOf(item.id)==-1;
});

Atnoher solution is to use filter method which accepts a callback function, applied to every item in the array.

Read more here.

Upvotes: 3

Related Questions