I have an object containing product details with categories, colors, usage skills and price. Now, I want to find all objects by providing min price and max price from my object:
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
}];
Let say I provide minPrice = 100 and maxPrice = 190 so it should return me:
[{
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
}]
Is there any predefined lodash function to get this?
I have an object containing product details with categories, colors, usage skills and price. Now, I want to find all objects by providing min price and max price from my object:
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
}];
Let say I provide minPrice = 100 and maxPrice = 190 so it should return me:
[{
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
}]
Is there any predefined lodash function to get this?
Share Improve this question asked Apr 20, 2018 at 10:10 Faizan SaiyedFaizan Saiyed 8523 gold badges19 silver badges38 bronze badges4 Answers
Reset to default 5With lodash, you can use _.fliter() like that:
_.filter(products, function(p) {
return p.price >= 100 && p.price <= 190;
});
It will return a new array that contains all matching occurrences. According to the below example, you can store it in a variable and do whatever you want with it.
You can also wrap _.filter()
inside a function:
function filterProduct(minPrice, maxPrice) {
return _.filter(products, function(p) {
return p.price >= minPrice && p.price <= maxPrice;
});
}
Jsfiddle with your example here : https://jsfiddle/maximelafarie/kps08L5x/4/
Why not simply use filter
var minPrice = 100;
var maxPrice = 190;
var output = products.filter( s => s.price >= minPrice && s.price <= maxPrice );
Demo
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);
You can directly use filter
method of array to get required result
DEMO
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; }
In point free fashion, this should do the trick:
import { filter, lte, gte, flow, get, overEvery } from 'lodash/fp'
const filterProduct =
(min, max) =>
filter(flow(
get('price'),
overEvery(gte(min), lte(max)),
))
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744175640a4561739.html
评论列表(0条)