Is there a way to remove an object named Test1 from an array of objects? I am struggling with finding the correct method to achieve this. If there are any errors in my code, please feel free to point them out. I have utilized a service to update the values in the array. You can take a look at my stackblitz for further insight. When the "Remove Name" button is clicked, I aim to eliminate the object with the name Test1 from the array. Any assistance in solving this issue would be greatly appreciated.
dashboard.component.html:
<button (click)="callFun('Test1')">Remove Name</button>
{{ deleteItemName }}
<app-optymodel></app-optymodel>
dashboard.component.ts:
callFun(name: any) {
this.commonService.modifyDeleteItem(name);
this.commonService.modifyNameList(name);
this.commonService.modifyDeleteItem('');
}
common.service.ts:
modifyNameList(name: string) {
const nameList = this.nameList.getValue();
const result = nameList.filter((x) => x.name != name);
this.nameList.next(result);
}
modifyDeleteItem(name: string) {
this.deletedItem.next(name);
}
After clicking the "Remove Name" button, the expected result is as follows:
const result = nameList.filter((x) => x != name); console.log(result);
[ { name: 'Test2', id: '2' }, { name: 'Test3', id: '3' }, { name: 'Test4', id: '4' }, { name: 'Test5', id: '5' }, ];