在我的 angular2 应用程序中,其中包含复选框;复选框键存储在名为“ticketselected”的数组中,选定的复选框键存储在“selectedArray”中。我想将这些键一一分配给一个变量并使用该变量。我需要拼接一个数组。我该如何实现这个?
我所做的是:
组件.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;
});
另一个解决方案是使用filter
方法,该方法接受callback
函数,应用于数组
中的每个项目。
了解更多 here.
关于javascript - 如何在数组内部循环并分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43318706/