【问题标题】:lodash - Find all objects by providing with two values to find withinlodash - 通过提供两个值来查找所有对象
【发布时间】:2018-04-20 10:10:57
【问题描述】:

我有一个包含产品详细信息的对象,包括类别、颜色、使用技巧和价格。 现在,我想通过提供对象的最低价格和最高价格来查找所有对象:

const products = [{
      name: 'Product 1',
      usage: 'Home',
      skill: 'Easy',
      color: 'blue',
      price: 100.00
    }, {
      name: 'Product 2',
      usage: 'Home',
      skill: 'Intermediate',
      color: 'red',
      price: 120.00
    }, {
      name: 'Product 3',
      usage: 'Office',
      skill: 'Intermediate',
      color: 'green',
      price: 190.00
    }, {
      name: 'Product 4',
      usage: 'Office',
      skill: 'Advanced',
      color: 'blue',
      price: 260.00
    }, {
      name: 'Product 5',
      usage: 'Warehouse',
      skill: 'Advanced',
      color: 'white',
      price: 320.00
    }, {
      name: 'Product 6',
      usage: 'Farm',
      skill: 'Intermediate',
      color: 'red',
      price: 120.00
    }, {
      name: 'Product 7',
      usage: 'Space',
      skill: 'Advanced',
      color: 'green',
      price: 150.00
    }, {
      name: 'Product 8',
      usage: 'Bathroom',
      skill: 'Easy',
      color: 'black',
      price: 9.00
    }];

假设我提供 minPrice = 100 和 maxPrice = 190 所以它应该返回我:

[{
  name: 'Product 1',
  usage: 'Home',
  skill: 'Easy',
  color: 'blue',
  price: 100.00
}, {
  name: 'Product 2',
  usage: 'Home',
  skill: 'Intermediate',
  color: 'red',
  price: 120.00
}, {
  name: 'Product 3',
  usage: 'Office',
  skill: 'Intermediate',
  color: 'green',
  price: 190.00
}, {
  name: 'Product 6',
  usage: 'Farm',
  skill: 'Intermediate',
  color: 'red',
  price: 120.00
}, {
  name: 'Product 7',
  usage: 'Space',
  skill: 'Advanced',
  color: 'green',
  price: 150.00
}]

是否有任何预定义的 lodash 函数来获得这个?

【问题讨论】:

    标签: javascript json object lodash


    【解决方案1】:

    使用 lodash,您可以像这样使用_.fliter()

    _.filter(products, function(p) {
      return p.price >= 100 && p.price <= 190;
    });
    

    它将返回一个包含所有匹配项的新数组。根据the below example,你可以将它存储在一个变量中,然后用它做任何你想做的事情。

    您也可以将_.filter() 包装在一个函数中:

    function filterProduct(minPrice, maxPrice) {
      return _.filter(products, function(p) {
        return p.price >= minPrice && p.price <= maxPrice;
      });
    }
    

    Jsfiddle 在这里举个例子:https://jsfiddle.net/maximelafarie/kps08L5x/4/

    【讨论】:

      【解决方案2】:

      为什么不简单地使用filter

      var minPrice = 100;
      var maxPrice = 190;
      var output = products.filter( s => s.price >= minPrice && s.price <= maxPrice );
      

      演示

      const products = [{
        name: 'Product 1',
        usage: 'Home',
        skill: 'Easy',
        color: 'blue',
        price: 100.00
      }, {
        name: 'Product 2',
        usage: 'Home',
        skill: 'Intermediate',
        color: 'red',
        price: 120.00
      }, {
        name: 'Product 3',
        usage: 'Office',
        skill: 'Intermediate',
        color: 'green',
        price: 190.00
      }, {
        name: 'Product 4',
        usage: 'Office',
        skill: 'Advanced',
        color: 'blue',
        price: 260.00
      }, {
        name: 'Product 5',
        usage: 'Warehouse',
        skill: 'Advanced',
        color: 'white',
        price: 320.00
      }, {
        name: 'Product 6',
        usage: 'Farm',
        skill: 'Intermediate',
        color: 'red',
        price: 120.00
      }, {
        name: 'Product 7',
        usage: 'Space',
        skill: 'Advanced',
        color: 'green',
        price: 150.00
      }, {
        name: 'Product 8',
        usage: 'Bathroom',
        skill: 'Easy',
        color: 'black',
        price: 9.00
      }];
      
      var minPrice = 100;
      var maxPrice = 190;
      var output = products.filter(s => s.price >= minPrice && s.price <= maxPrice);
      
      console.log(output);

      【讨论】:

        【解决方案3】:

        你可以直接使用数组的filter方法得到需要的结果

        演示

        const products = [{
              name: 'Product 1',
              usage: 'Home',
              skill: 'Easy',
              color: 'blue',
              price: 100.00
            }, {
              name: 'Product 2',
              usage: 'Home',
              skill: 'Intermediate',
              color: 'red',
              price: 120.00
            }, {
              name: 'Product 3',
              usage: 'Office',
              skill: 'Intermediate',
              color: 'green',
              price: 190.00
            }, {
              name: 'Product 4',
              usage: 'Office',
              skill: 'Advanced',
              color: 'blue',
              price: 260.00
            }, {
              name: 'Product 5',
              usage: 'Warehouse',
              skill: 'Advanced',
              color: 'white',
              price: 320.00
            }, {
              name: 'Product 6',
              usage: 'Farm',
              skill: 'Intermediate',
              color: 'red',
              price: 120.00
            }, {
              name: 'Product 7',
              usage: 'Space',
              skill: 'Advanced',
              color: 'green',
              price: 150.00
            }, {
              name: 'Product 8',
              usage: 'Bathroom',
              skill: 'Easy',
              color: 'black',
              price: 9.00
            }]
        
        console.log(products.filter(({price})=>price>=100&&price<=190))
        .as-console-wrapper { max-height: 100% !important; top: 0; }

        【讨论】:

          【解决方案4】:

          以无点的方式,这应该可以解决问题:

          import { filter, lte, gte, flow, get, overEvery } from 'lodash/fp'
          
          const filterProduct =
            (min, max) => 
              filter(flow(
                get('price'),
                overEvery(gte(min), lte(max)),
              ))
          

          【讨论】:

            猜你喜欢
            • 2018-07-16
            • 2021-10-04
            • 2015-06-03
            • 2017-05-10
            • 1970-01-01
            • 1970-01-01
            • 2020-01-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多