簡體   English   中英

如何在數組內部循環並分配給變量

[英]How to loop inside array and assign to a variable

在我的angular2應用程序中,其中包含復選框; 復選框鍵存儲在名為“ ticketselected”的數組中,選中的復選框鍵存儲在“ selectedArray”中。 我想將這些鍵一個一個地分配給一個變量並使用該變量。 我需要拼接一個數組。 我該如何實施?

我所做的是:

component.ts

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

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

' selectedArray '包含所有選定的鍵。

我應該如何實施呢?

編輯

 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);
                    }
                    });
                })

您應該使用indexOf方法。

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

Atnoher解決方案是使用接受callback函數的filter方法,該方法應用於array每個項目。

在這里閱讀更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM