取消選中復選框后,我試圖從陣列中洗掉一行。
陣列結構如下:
this.SourceArrayData:[
{cID: 0, cType: "Type1", cName: " Name1"},
{cID: 1, cType: "Type2", cName: " Name2"},
{cID: 2, cType: "Type3", cName: " Name3"}
]
Need to remove -> {cID: 1, cType: "Type2", cName: " Name2"} from the this.SourceArrayData and
so, i should get the final array as
this.SourceArrayData:[
{cID: 0, cType: "Type1", cName: " Name1"},
{cID: 2, cType: "Type3", cName: " Name3"}
]
我嘗試了很多方法,但它不起作用。當我執行我的代碼時,我沒有得到包含 2 行的最終陣列,而是只得到洗掉的陣列項。有誰能夠幫助我?
removeFromArray(arry: any, value: any) {
array.forEach((element, index) => {
if (element.testID === value.testID) {
arry.splice(index, 1)
}
});
return array;
}
Getting the value like below from the unchecking the checkbox
arrayObject: {cID: 1, cType: "Type2", cName: "Name2"}
if (event.selectAll !== null ) {
this.removeFromArray(this.SourceArrayData, arrayObject);
}
uj5u.com熱心網友回復:
試試這個方法
let SourceArrayData = [
{ cID: 0, cType: "Type1", cName: " Name1" },
{ cID: 1, cType: "Type2", cName: " Name2" },
{ cID: 2, cType: "Type3", cName: " Name3" }
];
function removeFromArray(arry: any[], value: any) {
SourceArrayData = arry.filter(item => item.cID !== value.cID);
}
const arrayObject = {cID: 1, cType: "Type2", cName: "Name2"}
if (event.selectAll !== null) {
removeFromArray(SourceArrayData, arrayObject);
}
console.log(SourceArrayData)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367275.html
上一篇:是否可以改進此陣列交集代碼
