【问题标题】:Searching javascript array of objects based on multiple predicates (or) - lodash基于多个谓词(或)搜索对象的javascript数组 - lodash
【发布时间】: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


    【解决方案1】:

    您可以将比较器函数作为第二个参数传递,您可以在其中放置您想要的任何逻辑:

    let toPush = _.findIndex(result, function (item) {
      // put logic in here, return true if the item matches or false otherwise
      return (item.itemNumber === queryArray[j][0] || item.listings.some(listing => listing.urlId === queryArray[j][0]));
    });
    

    这实际上与原版 JS 相同:

    let toPush = result.findIndex(function (item) {
      // put logic in here, return true if the item matches or false otherwise
      return (item.itemNumber === queryArray[j][0] || item.listings.some(listing => listing.urlId === queryArray[j][0]));
    });
    

    【讨论】:

    • Listings 是一个嵌套的对象数组,我试图避免一堆 if 语句和循环。我目前有 3 个谓词,但将来可能需要添加更多。
    • 哦,所以你需要一种方法来挖掘子数组,比如上面使用的Array.some。也可能有一些 lodash 版本。
    【解决方案2】:

    我将使用的带有模式的可运行代码:

    const result = [1,2,3,4,5,6,7,8,9,0]
    
    console.log(result.findIndex((x) => [3,5].some((y) => x == y)))

    当然,您需要将[3,5] 替换为您希望检查的值。

    【讨论】:

      【解决方案3】:

      Loadash 方法

      let users = [
        { 'user': 'barney',  'age': 36, 'active': true },
        { 'user': 'fred',    'age': 40, 'active': false },
        { 'user': 'pebbles', 'age': 1,  'active': true }
      ];
      let result = _.find(users, function(u) {
        return u.age <= 40 && u.active; 
      });
      console.log(result);

      注意请注意,find 函数如果没有找到,将返回 undefined

      ES16 方法

      let users = [
        { 'user': 'barney',  'age': 36, 'active': true },
        { 'user': 'fred',    'age': 40, 'active': false },
        { 'user': 'pebbles', 'age': 1,  'active': true }
      ];
      
      let result = users.find(function(u){
        return u.age<=40 && u.active && u.user.includes('r');
      });
      
      console.log(result);

      注意像 lodash find 函数 ES find 如果没有找到也会返回 undefined。

      【讨论】:

        猜你喜欢
        • 2019-05-06
        • 1970-01-01
        • 2017-07-16
        • 2016-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多