0

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)
2
  • 2
    But sales is an array right. If even one of the sales from product is less than 5 you want to show it or only if all of them? Commented Oct 12, 2021 at 19:21
  • 1
    Minor note < 4 is not the same as < 5 Commented Oct 12, 2021 at 19:23

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Erik. in case of both amounts must be less than 5, how I could do it?
0

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);

2 Comments

Hi The fourth bird, can you fix this filter? I just add one more element "customers" `
@TulioVargas You unselected the mark as answer, but the provided answer is for the related question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.