javascript - JS Array.find 如果未定义的元素
问题描述
我尝试合并两个数组:
productsAll = parserArrayFull.map((item) => {
return {
...item,
...firestoreArrayFull.find((y) => y.productSKU === item.SELLER_SKU),
};
});
如果在“firestoreArrayFull”中有与 item.SELLER_SKU 具有相同值的“productSKU”,则一切正常。但如果没有错误“无法读取未定义的属性'productSKU'”如何为这种情况制定条件?我只想在出错的情况下保留“项目”。问候
解决方案
看起来您在firestoreArrayFull
数组中有一些空值。
在进行比较之前尝试 puty &&
以确保y
isnull
或undefined
.
productsAll = parserArrayFull.map((item) => {
return {
...item,
...firestoreArrayFull.find((y) => y && y.productSKU === item.SELLER_SKU),
};
});
更新
- 如果未找到,则添加
productSKU
等于 的属性item.SELLER_SKU
。
productsAll = parserArrayFull.map((item) => {
return {
...item,
...(firestoreArrayFull.find((y) => y && y.productSKU === item.SELLER_SKU) || { productSKU: item.SELLER_SKU }),
};
});
推荐阅读
- angular - 在第二次访问路由之前,AngularFireDocument 不响应路由更改
- php - 删除 .php 或 .html 后的尾部斜线
- jestjs - 什么时候应该使用 setupFiles 而不是 setupFilesAfterEnv?
- php - 如何通过 php sdk 代码获取docusign“代码”值?
- nhibernate - nhibernate 5之后的不同查询(使用oracle)
- orientdb - OrientDB SQL 中等效的 Tinkerpop path() 步骤
- angular - 如何在 Angular 6 中设置 url 中的参数?
- windows - 如何在不使用引号的情况下在 echo 中使用 &&?
- c# - 关于C# Expression.MakeBinary(),如何使用
- php - 在 wordpress 后端为自定义帖子、分类和元创建导出到 CSV 按钮