Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

98
Visualizações
Filter and MAP Object inside Object in Javascript

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)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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));
about 4 years ago · Juan Pablo Isaza Relatório

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda