我正在使用带有 JavaScript 的 Vue.js。
我有一个名为 products
的对象数组每个对象都有 property
调用smallest_unit_barcode
.我只想过滤带有条码的产品,例如 value
,所以我做了这个功能:
if (value != '') {
var results = this.products.filter(obj=>obj.smallest_unit_barcode.includes(value));
var results = results.slice(Math.max(results.length - 20, 0))
this.pos_quick_lunch = results;
}
一切正常,但如果 obj.smallest_unit_barcode == null
,我得到这个错误:Error in v-on handler: "TypeError: Cannot read property 'includes' of null"
如何忽略 null
过滤产品数组时的值?
最佳答案
比较 null
在您尝试访问该属性之前:
obj => obj.smallest_unit_barcode !== null && obj.smallest_unit_barcode.includes(value)
因为&&
是 short circuiting ,如果左操作数的计算结果为假,则不会计算右操作数。
关于javascript - 无法读取 null 的属性 'includes'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62714171/