如何循环内部数组并分配给变量

时间:2017-04-10 08:51:54

标签: javascript angularjs arrays function

在我的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);
                    }
                    });
                })

1 个答案:

答案 0 :(得分:3)

您应该使用indexOf方法。

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

Atnoher解决方案是使用filter方法接受callback函数,该方法应用于array中的每个项目。

了解更多here.

相关问题