【发布时间】:2022-07-15 15:35:14
【问题描述】:
我正在使用 find 方法来提取 ID(字符串),但这会返回一个未定义的,因为它不存在。
const additionalLinePhoneNumber = products.find(product => product.name === 'segundaLinea').id;
产品有以下:
(2) [ProductInventoryList, ProductInventoryList]
0: ProductInventoryList {_id: "12345", _name: "lineaFija", _productInventoryCharacteristics: ProductInventoryCharacteristics}
1: ProductInventoryList {_id: "12345", _name: "primeraLinea", _productInventoryCharacteristics: ProductInventoryCharacteristics}
length: 2
所以“segundaLinea”没有返回,所以查找给了我以下错误:
ERROR 错误:未捕获(承诺中):TypeError:无法读取未定义的属性“id” 类型错误:无法读取未定义的属性“id”
我试过这样做但没有用:
const additionalLinePhoneNumber = products.find(product => product.name === 'segundaLinea').id ? undefined : '';
我错过了什么?
试试下面的遮阳篷:
【问题讨论】:
标签: typescript angular5