0

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);
                    }
                    });
                })
1
  • First of all, replace entry == true with entry === true if you want to check the value of entry. Otherwise, that condition is going to be true for any truthy value. Commented Apr 10, 2017 at 8:56

1 Answer 1

3

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.

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

13 Comments

thanks for your quick reply. i use this indexOf method. but it always return -1 . am adding my edited code.
@NidhiMalhotra, you have syntax error in your edit. Maybe it is a mistake.
This line :if (index != -1){allTickets); must be if (index != -1){
Please tell me you is the structure of allTickets
all tickets is array of objects like :[object, object, object]
|

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.