How I can use the filter to populate the variable onlyAmountLess5? I want to filter only products where sales' amount < 5:
const products = [
{ id: 1, name: "bread", value: 2.0, category: "bakery", "sales":[{"day": "07/04/2021", "amount": 12}, {"day": "10/04/2021", "amount": 18}] },
{ id: 2, name: "apple", value: 6.5, category: "fruit", "sales":[{"day": "07/04/2021", "amount": 4}, {"day": "10/04/2021", "amount": 18}] },
{ id: 3, name: "pizza", value: 2.0, category: "food", "sales":[{"day": "07/04/2021", "amount": 5}, {"day": "10/04/2021", "amount": 18}]} ,
{ id: 4, name: "cheese", value: 7.0, category: "bakery", "sales":[{"day": "07/04/2021", "amount": 12}, {"day": "10/04/2021", "amount": 5}] },
{ id: 5, name: "milk", value: 2.2, category: "food", "sales":[{"day": "07/04/2021", "amount": 12}, {"dia": "10/04/2021", "amount": 3}]}
];
const onlyBakery = products.filter( p => p.category ==="bakery")
console.log(onlyBakery);
// List only product where sales amount < 5
const onlyAmountLess5 = products.filter(o => products.sales.amount < 4)
The field sales has two amounts. If you want to check them all you can do it like this:
const maxAmount = 5;
const onlyAmountLess5 = products.filter(product => product.sales.some(item => item.amount < maxAmount));
If you want to select the entry where all the amounts are less than 5, you can use every
Note that it would give an empty array, as for none of the example all objects have an amount lesser than 5.
Mind that < 4 is not the same as <5. You could also write <=4 instead.
const products = [
{ id: 1, name: "bread", value: 2.0, category: "bakery", "sales":[{"day": "07/04/2021", "amount": 12}, {"day": "10/04/2021", "amount": 18}] },
{ id: 2, name: "apple", value: 6.5, category: "fruit", "sales":[{"day": "07/04/2021", "amount": 4}, {"day": "10/04/2021", "amount": 18}] },
{ id: 3, name: "pizza", value: 2.0, category: "food", "sales":[{"day": "07/04/2021", "amount": 5}, {"day": "10/04/2021", "amount": 18}]} ,
{ id: 4, name: "cheese", value: 7.0, category: "bakery", "sales":[{"day": "07/04/2021", "amount": 12}, {"day": "10/04/2021", "amount": 5}] },
{ id: 5, name: "milk", value: 2.2, category: "food", "sales":[{"day": "07/04/2021", "amount": 12}, {"dia": "10/04/2021", "amount": 3}]}
];
const onlyAmountLess5 = products.filter(o =>o.sales.every(i => i.amount < 5));
console.log(onlyAmountLess5);
An example using < 13 to return 2 entries:
const products = [
{ id: 1, name: "bread", value: 2.0, category: "bakery", "sales":[{"day": "07/04/2021", "amount": 12}, {"day": "10/04/2021", "amount": 18}] },
{ id: 2, name: "apple", value: 6.5, category: "fruit", "sales":[{"day": "07/04/2021", "amount": 4}, {"day": "10/04/2021", "amount": 18}] },
{ id: 3, name: "pizza", value: 2.0, category: "food", "sales":[{"day": "07/04/2021", "amount": 5}, {"day": "10/04/2021", "amount": 18}]} ,
{ id: 4, name: "cheese", value: 7.0, category: "bakery", "sales":[{"day": "07/04/2021", "amount": 12}, {"day": "10/04/2021", "amount": 5}] },
{ id: 5, name: "milk", value: 2.2, category: "food", "sales":[{"day": "07/04/2021", "amount": 12}, {"dia": "10/04/2021", "amount": 3}]}
];
const onlyAmountLess5 = products.filter(o =>o.sales.every(i => i.amount < 13));
console.log(onlyAmountLess5);