Home > database >  Array remove issue angular 8
Array remove issue angular 8

Time:11-27

I'm attempting to delete a row from an array after unchecking a checkbox.

The array structure is as follows:

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"}
]
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

I tried many ways but its not working. When i execute my code, i am not getting the final array with 2 rows instead getting the removed array items only. Can anybody help me?

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);
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

try this way

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)
  • Related