javascript - 从数组中删除某些项目
问题描述
我正在努力从数组中删除一个项目。该项目需要动态删除。
这是我的代码
大批
{
"corporateId": "be67e184-a663-439c-b841-c14a734011eb",
"selectedMAP": [
{
"mapId": 79,
"mapName": "discovery",
"active": true,
"options": [
{
"optionId": 116,
"optionName": "comprehensive",
"memberAmount": 2000,
"adultDependantAmount": 1500,
"childDependantAmount": 500,
"active": true
}
]
},
{
"mapId": 80,
"mapName": "goodhealth",
"active": true,
"options": [
{
"optionId": 117,
"optionName": "keycore",
"memberAmount": 1000,
"adultDependantAmount": 600,
"childDependantAmount": 400,
"active": true
},
{
"optionId": 119,
"optionName": "keycore2",
"memberAmount": 500,
"adultDependantAmount": 300,
"childDependantAmount": 200,
"active": true
}
]
}
]
}
TS
removeOption(index: number, indexOption: number) {
this.companyMedicalAidProvider[0].selectedMAP[index].options[indexOption].splice(indexOption, 1);
}
HTML
<div id="medicalCard" class="medicalCard" *ngFor="let provider of companyMedicalAidProvider; let i = index;">
...
<div id="option" class="row option" *ngFor="let providerOptions of companyMedicalAidProvider[0].selectedMAP[i].options; let b = index;">
<button (click)="removeOption(b,i)">X</button>
</div>
...
</div>
我在控制台中看到的错误是ERROR TypeError: this.companyMedicalAidProvider[0].selectedMAP[index].options[indexOption].splice is not a function
解决方案
您必须在数组上调用 splice 而不是在对象本身上调用。将其更改为options.splice
应该可以工作:
removeOption(index: number, indexOption: number) {
this.companyMedicalAidProvider[0].selectedMAP[index].options.splice(
indexOption,
1
);
}
推荐阅读
- laravel - Laravel Spark 9.0 未定义的货币标签
- google-apps-script - Mailsender 在手动运行时运行良好,而在时间触发时使用非实际数据
- jquery - 使用 jQuery 按数据标题对项目进行排序
- python - 如何从python中的字典列表中提取特定值
- c - 正确使用转换说明符 n
- html - 如何更改和添加适用于 iOS 的 GDPR 同意表.html 文件
- d3.js - 无法在力有向图上向节点添加标签
- c# - 端到端测试中的依赖注入
- r - 如何修复我的 XTS(时间序列)以与我的 CSV 文件相关联?
- css - 0 padding 与 1px 的不同行为