lodash - 通过提供两个值来查找所有对象

时间:2018-04-20 10:10:58

标签: javascript json object lodash

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

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函数来获取此功能?

4 个答案:

答案 0 :(得分:2)

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

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

它将返回一个包含所有匹配事件的新数组。根据{{​​3}},您可以将其存储在变量中并随意执行任何操作。

您还可以将_.filter()包裹在函数中:

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

在这里举例说明你的例子:the below example

答案 1 :(得分:0)

为什么不简单地使用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);

答案 2 :(得分:0)

您可以直接使用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; }

答案 3 :(得分:0)

在免费方式中,这应该可以解决问题:

import { filter, lte, gte, flow, get, overEvery } from 'lodash/fp'

const filterProduct =
  (min, max) => 
    filter(flow(
      get('price'),
      overEvery(gte(min), lte(max)),
    ))