lodash - 查找数组索引失败
问题描述
我有一个组件数组(this.needle),其中一些但并非全部都有一个现有的 RMA,它是组件对象内的一个 RMA 对象。我正在尝试对 RMA 的删除进行编码。该方法接收记录,所以我知道 obj 有一个 rma 对象。
针数组是一个对象数组,如下所示:
[
{
cmp_auto_key: 3047,
company_code: "FCAS2",
condition_code: "AR",
description: "PUMP, ELECT MOTOR DRIVEN",
invc_number: "87259",
msn: '11105",
rma: {
contact: "jsmith@co.com",
disposition: 1,
id: 714,
status_id: 3,
submit_deadline: "2019-11-08 00:00:00"
}
},
{
cmp_auto_key: 3047,
company_code: "FCAS2",
condition_code: "AR",
description: "PUMP, ELECT MOTOR DRIVEN",
invc_number: "87259",
msn: '11111"
}
]
这是删除代码:
if(confirm('Are you sure you want to delete this RMA?')) {
let id = obj.rma.id;
let idx = _.findIndex(this.needle, {id: obj.rma.id}); //produces error
console.log('KILLER', this.needle[idx]);
this.needle[idx].rma = null;
console.log('KILLED', this.needle[idx]);
} // end if
此代码在指示的行上终止,因为并非数组中的所有元素都具有 RMA 对象,因此它返回
“无法设置未定义的属性 'rma'”
如果没有 rma 对象而不抛出异常,有没有办法让 findIndex 一起忽略记录?
非常感谢任何指导。
解决方案
推荐阅读
- javascript - 找不到模块:无法解析“firebase”
- nuget - 意外行为 nuget + ContentFiles/any/any
- python - TensorFlow自定义损失函数错误:“AttributeError:'NoneType'对象没有属性'op'”
- sql - 在 SQL Server 中,如何检查列值是否仅包含零
- ruby-on-rails - 使用 Ransack 进行多态关联搜索,可以吗?
- java - 使用 ByteBuddy 拦截未实现的接口方法
- angular - 具有公共节点的 Apache Sankey 图表
- javascript - 用 react.js 连接 Django
- pandas - 具有字符串值的两个数据帧的逻辑“或”
- java - 用于从本地数据库获取代码的两因素身份验证脚本