【发布时间】:2021-10-02 14:07:21
【问题描述】:
我有一个对象数组,我想搜索这些对象以找到匹配的结果。目前我正在使用 lodash,我的查询如下所示:
let toPush = _.findIndex(result, {itemNumber: queryArray[j][0]})
我想编辑谓词以搜索多个属性,但使用 or 谓词,所以它看起来像:
let toPush = _.findIndex(result, {itemNumber: queryArray[j][0] || listings.urlId : queryArray[j][0]})
如何在 lodash 中使用 or 运算符?
我不一定要用lodash vs vanilla js,也可以代替索引,返回整个对象。
编辑
这就是我最终的结果:
let toPush = _.findIndex(result, {listings: [{urlId: queryArray[j][0]}]}) > -1
? _.findIndex(result, {listings: [{urlId: queryArray[j][0]}]})
:_.findIndex(result, {itemNumber:queryArray[j][0]}) > -1
? _.findIndex(result, {itemNumber:queryArray[j][0]})
: _.findIndex(result, {productIdentifiers: [{productIdentifier: queryArray[j][0]}]}) > -1
? _.findIndex(result, {productIdentifiers: [{productIdentifier: queryArray[j][0]}]})
: -1
如果有人能以更好的方式提出意见,我将不胜感激。根据到目前为止的答案,我还没有其他任何工作。
谢谢。
【问题讨论】:
标签: javascript node.js lodash